<?php

namespace App\Console;

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel
{
    /**
     * Define the application's command schedule.
     */
    protected function schedule(Schedule $schedule): void
    {
        // Optionally prune old previews daily
        if (config('files.enable_preview_prune', true)) {
            $days = (int) config('files.preview_retention_days', 90);
            if ($days < 1) {
                $days = 90;
            }
            $schedule->command('documents:prune-previews', [
                '--days' => $days,
            ])->dailyAt('02:00');
        }

        // Optional: refresh configured materialized views for reporting
        $views = (array) config('reports.materialized_views', []);
        if (! empty($views)) {
            $time = (string) (config('reports.refresh_time', '03:00') ?: '03:00');
            $schedule->command('reports:refresh-mviews', [
                '--concurrently' => true,
            ])->dailyAt($time);
        }
    }

    /**
     * Register the commands for the application.
     */
    protected function commands(): void
    {
        $this->load(__DIR__.'/Commands');

        require base_path('routes/console.php');
    }
}
