<?php

declare(strict_types=1);

namespace App\Livewire\Admin;

use Filament\Actions\Action;
use Filament\Actions\Concerns\InteractsWithActions;
use Filament\Actions\Contracts\HasActions;
use Filament\Notifications\Notification;
use Filament\Schemas\Concerns\InteractsWithSchemas;
use Filament\Schemas\Contracts\HasSchemas;
use Filament\Support\Enums\Size;
use Filament\Support\Icons\Heroicon;
use Illuminate\Contracts\View\View;
use Illuminate\Support\Facades\Artisan;
use Livewire\Component;

class ClearCacheButton extends Component implements HasActions, HasSchemas
{
    use InteractsWithActions;
    use InteractsWithSchemas;

    public function clearCacheAction(): Action
    {
        return Action::make('clearCache')
            ->label(__('admin.nav.cache'))
            ->icon(Heroicon::OutlinedTrash)
            ->color('gray')
            ->button()
            ->outlined()
            ->size(Size::Small)
            ->requiresConfirmation()
            ->modalHeading(__('admin.pages.cache_title'))
            ->modalDescription(__('admin.pages.cache_confirm'))
            ->modalSubmitActionLabel(__('admin.pages.cache_run'))
            ->action(function (): void {
                Artisan::call('cache:clear');
                Artisan::call('view:clear');
                Artisan::call('route:clear');
                Artisan::call('config:clear');

                Notification::make()
                    ->title(__('admin.pages.cache_cleared'))
                    ->success()
                    ->send();
            });
    }

    public function render(): View
    {
        return view('livewire.admin.clear-cache-button');
    }
}
