<?php

declare(strict_types=1);

namespace TechnoArtisan\FilamentCms\Commands;

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

class CacheAllCommand extends Command
{
    public $signature = 'filament-cms:optimize {clear?}';

    public $description = 'optimize complete Laravel + packages';

    public function handle(): int
    {
        return ($this->argument('clear')) ? $this->clear() : $this->cache();
    }

    private function cache(): int
    {
        $this->comment('caching + optimizing');
        $this->comment('');

        $this->comment('config:cache...');
        Artisan::call('config:cache');

        $this->comment('event:cache...');
        Artisan::call('event:cache');

        $this->comment('filament:cache-components...');
        Artisan::call('filament:cache-components');

        $this->comment('route:cache...');
        Artisan::call('route:cache');

        $this->comment('view:cache...');
        Artisan::call('view:cache');

        $this->comment('optimize...');
        Artisan::call('optimize');

        return self::SUCCESS;
    }

    private function clear(): int
    {
        $this->comment('clearing cache');
        $this->comment('');

        $this->comment('config:clear...');
        Artisan::call('config:clear');

        $this->comment('event:clear...');
        Artisan::call('event:clear');

        $this->comment('filament:cache-components...');
        Artisan::call('filament:cache-components');
        Artisan::call('filament:optimize-clear');

        $this->comment('route:clear...');
        Artisan::call('route:clear');

        $this->comment('view:clear...');
        Artisan::call('view:clear');

        $this->comment('optimize:clear...');
        Artisan::call('optimize:clear');

        return self::SUCCESS;
    }
}

/*
  cache:clear                              Flush the application cache
  cache:forget                             Remove an item from the cache
  cache:prune-stale-tags                   Prune stale cache tags from the cache (Redis only)
  config:cache                             Create a cache file for faster configuration loading
  config:clear                             Remove the configuration cache file
  event:cache                              Discover and cache the application's events and listeners
  event:clear                              Clear all cached events and listeners
  filament:cache-components                Cache all components
  filament:clear-cached-components         Clear all cached components
  filament:optimize-clear                  Remove the cached components and Blade icons
  icons:cache                              Discover icon sets and generate a manifest file
  make:cache-table                         [cache:table] Create a migration for the cache database table
  optimize:clear                           Remove the cached bootstrap files
  package:discover                         Rebuild the cached package manifest
  route:cache                              Create a route cache file for faster route registration
  route:clear                              Remove the route cache file
  schedule:clear-cache                     Delete the cached mutex files created by scheduler
  view:cache                               Compile all of the application's Blade templates

 */
