<?php

declare(strict_types=1);

namespace App\Filament\Pages;

use App\Models\Setting;
use BackedEnum;
use Filament\Actions\Action;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Notifications\Notification;
use Filament\Pages\Page;
use Filament\Schemas\Components\Actions;
use Filament\Schemas\Components\Form;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Schema;
use Filament\Support\Icons\Heroicon;

/**
 * @property-read Schema $form
 */
class HealthSettings extends Page
{
    protected static string|BackedEnum|null $navigationIcon = Heroicon::OutlinedCog6Tooth;

    protected static ?string $navigationLabel = 'Health Settings';

    protected static ?int $navigationSort = 510;

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

    public static function isDiscovered(): bool
    {
        return false;
    }

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

    public function mount(): void
    {
        $this->form->fill([
            'health_check_interval' => (string) Setting::get('health_check_interval', '30'),
            'health_alert_email' => Setting::get('health_alert_email'),
        ]);
    }

    public function form(Schema $schema): Schema
    {
        return $schema
            ->components([
                Form::make([
                    Section::make('Health Monitoring')
                        ->description('Control how often the application health checks run.')
                        ->schema([
                            TextInput::make('health_alert_email')
                                ->label('Alert Email Address')
                                ->helperText('Email address to send health check failure notifications to.')
                                ->email()
                                ->nullable()
                                ->placeholder('alerts@example.com'),
                            Select::make('health_check_interval')
                                ->label('Health Check Interval')
                                ->helperText('How often (in minutes) the health checks should run. Changes take effect on the next scheduler run.')
                                ->options([
                                    '1' => '1 minute',
                                    '5' => '5 minutes',
                                    '10' => '10 minutes',
                                    '15' => '15 minutes',
                                    '30' => '30 minutes',
                                    '45' => '45 minutes',
                                    '60' => '60 minutes',
                                ])
                                ->default('5')
                                ->required(),
                        ]),
                ])
                    ->livewireSubmitHandler('save')
                    ->footer([
                        Actions::make([
                            Action::make('save')
                                ->label('Save Settings')
                                ->submit('save')
                                ->keyBindings(['mod+s']),
                        ]),
                    ]),
            ])
            ->statePath('data');
    }

    public function save(): void
    {
        $data = $this->form->getState();

        Setting::set('health_check_interval', $data['health_check_interval'], 'integer', 'health');
        Setting::set('health_alert_email', $data['health_alert_email'] ?? null, 'string', 'health');

        Notification::make()
            ->title('Health settings saved')
            ->success()
            ->send();
    }
}
