<?php

namespace App\Filament\Pages;

use BackedEnum;
use Filament\Actions\Action;
use Filament\Forms\Components\KeyValue;
use Filament\Forms\Components\Repeater;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
use Filament\Notifications\Notification;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Schema;
use Filament\Support\Icons\Heroicon;
use Illuminate\Support\Facades\Artisan;
use Inerba\DbConfig\AbstractPageSettings;

class WebsiteSettings extends AbstractPageSettings
{
    /**
     * @var array<string, mixed> | null
     */
    public ?array $data = [];

    protected static ?string $title = 'Website Settings';

    protected static string|BackedEnum|null $navigationIcon = Heroicon::OutlinedCog6Tooth; // Uncomment if you want to set a custom navigation icon

    // protected ?string $subheading = ''; // Uncomment if you want to set a custom subheading

    // protected static ?string $slug = 'website-settings'; // Uncomment if you want to set a custom slug

    protected string $view = 'filament.pages.website-settings';

    protected function settingName(): string
    {
        return 'website';
    }

    /**
     * Provide default values.
     *
     * @return array<string, mixed>
     */
    public function getDefaultData(): array
    {
        return [];
    }

    public function getHeaderActions(): array
    {
        return [
            Action::make('filament-optimize')
                ->label('Optimize Application')
                ->color('gray')
                ->icon('heroicon-o-paper-airplane')
                ->action(fn () => $this->appOptimize()),

            Action::make('filament-optimize-clear')
                ->label('Clear Optimized Files')
                ->color('danger')
                ->icon('heroicon-o-trash')
                ->action(fn () => $this->appOptimizeClear()),

            ...parent::getHeaderActions(),
        ];
    }

    public function form(Schema $schema): Schema
    {
        return $schema
            ->components([
                Section::make('Website Information')
                    ->description('Change Basic Website Information')
                    ->collapsible()
                    ->schema([
                        TextInput::make('app_name')->label('Website Name')->required(),
                        TextInput::make('app_version')->label('Website Version')->required(),
                        TextInput::make('app_base_url')->label('Base URL')->required(),
                        TextInput::make('app_admin')->label('Website Admin')->email()->required(),
                        TextInput::make('app_contact')->label('Contact Email')->email()->required(),
                    ]),
                Section::make('Custom Codes')
                    ->description('Add custom codes to header and footer')
                    ->collapsed()
                    ->columns(2)
                    ->schema([
                        Textarea::make('app_header')->label('Custom Header')->rows(6)->columnSpan(1),
                        Textarea::make('app_footer')->label('Custom Footer')->rows(6)->columnSpan(1),
                    ]),
                Section::make('Meta Information')
                    ->description('Change Website Meta Information')
                    ->collapsed()
                    ->schema([
                        TextInput::make('app_title')->label('Title')->required(),
                        Textarea::make('app_description')->label('Description')->rows(3),
                        TextInput::make('app_keyword')->label('Keyword'),
                        KeyValue::make('app_meta')
                            ->label('Meta (Optional)')
                            ->keyPlaceholder('Name')
                            ->valuePlaceholder('Content'),
                    ]),
                Section::make('Social Links')
                    ->description('Enter your social links')
                    ->collapsed()
                    ->schema([

                        Repeater::make('app_social')
                            ->statePath('app_social')
                            ->schema([
                                TextInput::make('icon')->label('Icon')->required()->maxLength(100),
                                TextInput::make('url')->label('URL')->url()->required()->maxLength(255),
                            ]),

                    ]),
                Section::make('Ad Spaces')
                    ->description('You can setup your Ad Codes here for various Ad Spaces')
                    ->collapsed()
                    ->schema([
                        Textarea::make('ads_settings.one')->label('Ad Space 1')->rows(4),
                        Textarea::make('ads_settings.two')->label('Ad Space 2')->rows(4),
                        Textarea::make('ads_settings.three')->label('Ad Space 3')->rows(4),
                        Textarea::make('ads_settings.four')->label('Ad Space 4')->rows(4),
                        Textarea::make('ads_settings.five')->label('Ad Space 5')->rows(4),
                    ]),

            ])
            ->statePath('data');
    }

    private function appOptimize(): void
    {
        try {
            \Artisan::queue('optimize');
            Notification::make()
                ->title('App optimization successful!')
                ->success()
                ->send();
        } catch (\Exception $e) {
            \Log::error('App optimization failed', ['exception' => $e->getMessage()]);
            Notification::make()
                ->title('App optimization failed: '.$e->getMessage())
                ->danger()
                ->send();
        }
    }

    private function appOptimizeClear(): void
    {
        try {
            Artisan::queue('optimize:clear');
            Notification::make()
                ->title('Cache files clear successful!')
                ->success()
                ->send();
        } catch (\Exception $e) {
            \Log::error('App Optimize clear failed', ['exception' => $e->getMessage()]);
            Notification::make()
                ->title('Failed to clear cache files: '.$e->getMessage())
                ->danger()
                ->send();
        }
    }
}
