<?php

/**
 * ============================================================
 *
 * @project   biiproject
 *
 * @author    Andika Debi Putra
 *
 * @email     andikadebiputra@gmail.com
 *
 * @website   https://biiproject.com
 *
 * @copyright Copyright (c) 2026 Andika Debi Putra
 * @license   Proprietary - All Rights Reserved
 *
 * @version   1.0.0
 *
 * @created   2026-05-01
 * ============================================================
 *
 * Unauthorized copying, modification, distribution, or use
 * of this file is strictly prohibited without prior written
 * permission from the author.
 * ============================================================
 */

namespace App\Console\Commands;

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

class SystemOptimize extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'system:optimize';

    /**
     * The description of the console command.
     *
     * @var string
     */
    protected $description = 'Perform a full system optimization (Cache, Routes, Views, and Pruning) in one command.';

    /**
     * Execute the console command.
     */
    public function handle()
    {
        $this->info('🚀 Starting Full System Optimization...');

        $steps = [
            'Clearing Cache...' => 'cache:clear',
            'Caching Configuration...' => 'config:cache',
            'Caching Routes...' => 'route:cache',
            'Caching Views...' => 'view:cache',
            'Caching Events...' => 'event:cache',
            'Pruning Database Logs...' => 'model:prune',
        ];

        $bar = $this->output->createProgressBar(count($steps));
        $bar->start();

        foreach ($steps as $label => $command) {
            $this->line("\n".$label);
            try {
                Artisan::call($command);
                $this->info("✓ Done: {$command}");
            } catch (\Exception $e) {
                $this->error("✗ Failed: {$command}. Error: ".$e->getMessage());
            }
            $bar->advance();
        }

        $bar->finish();
        $this->line("\n");
        $this->info('✨ System Optimization Complete! Everything is running at peak performance.');

        return 0;
    }
}
