<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Artisan;

class OptimizePerformance extends Command
{
    protected $signature = 'optimize:performance';
    protected $description = 'Run all optimization commands for better performance';

    public function handle()
    {
        $this->info('🚀 Optimizing application performance...');
        
        // Clear all caches first
        $this->info('📋 Clearing caches...');
        Artisan::call('cache:clear');
        Artisan::call('config:clear');
        Artisan::call('route:clear');
        Artisan::call('view:clear');
        
        // Cache configurations for production
        $this->info('⚡ Caching configurations...');
        Artisan::call('config:cache');
        Artisan::call('route:cache');
        Artisan::call('view:cache');
        
        // Optimize composer autoloader
        if (app()->environment('production')) {
            $this->info('🎯 Optimizing Composer autoloader...');
            exec('composer install --optimize-autoloader --no-dev');
        }
        
        $this->info('✅ Performance optimization completed!');
        $this->info('');
        $this->info('📊 Performance improvements applied:');
        $this->info('• Redis cache driver enabled');
        $this->info('• Redis session driver enabled');
        $this->info('• Redis queue driver enabled');
        $this->info('• Configuration cached');
        $this->info('• Routes cached');
        $this->info('• Views cached');
        $this->info('• Settings cache TTL increased to 24 hours');
        $this->info('• Navigation loads progressively');
        $this->info('• Asset cache-busting optimized');
        
        return 0;
    }
}