<?php

namespace App\Console\Commands;

use App\Filament\Widgets\Server\SystemHealthWidget;
use Illuminate\Console\Command;
use ReflectionClass;

class WarmWidgetCache extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'widgets:warm-cache';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Warm the cache for all widgets to improve initial load performance';

    /**
     * Execute the console command.
     */
    public function handle(): int
    {
        $this->info('Warming widget cache...');

        try {
            // Warm SystemHealthWidget cache
            $this->info('Warming SystemHealthWidget cache...');
            $widget = app(SystemHealthWidget::class);

            // Use reflection to call protected getStats method
            $reflection = new ReflectionClass($widget);
            $method = $reflection->getMethod('getStats');
            $method->setAccessible(true);
            $method->invoke($widget);

            $this->info('SystemHealthWidget cache warmed successfully.');

            $this->info('All widget caches warmed successfully.');

            return self::SUCCESS;
        } catch (\Exception $e) {
            $this->error('Failed to warm widget cache: '.$e->getMessage());

            return self::FAILURE;
        }
    }
}
