<?php

declare(strict_types=1);

namespace App\Filament\Admin\Pages;

use App\Filament\Admin\Widgets\Dashboard\RecentActivityTable;
use App\Filament\Admin\Widgets\DashboardStatsWidget;
use App\Models\DnsSetting;
use BackedEnum;
use Filament\Actions\Action;
use Filament\Actions\Concerns\InteractsWithActions;
use Filament\Actions\Contracts\HasActions;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Concerns\InteractsWithForms;
use Filament\Forms\Contracts\HasForms;
use Filament\Pages\Page;
use Filament\Schemas\Components\EmbeddedTable;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Schema;
use Illuminate\Contracts\Support\Htmlable;

class Dashboard extends Page implements HasActions, HasForms
{
    use InteractsWithActions;
    use InteractsWithForms;

    protected static string|BackedEnum|null $navigationIcon = 'heroicon-o-home';

    protected static ?int $navigationSort = 1;

    protected static ?string $slug = 'dashboard';

    protected string $view = 'filament.admin.pages.dashboard';

    public static function getNavigationLabel(): string
    {
        return __('Dashboard');
    }

    public function getTitle(): string|Htmlable
    {
        return __('Dashboard');
    }

    protected function getHeaderWidgets(): array
    {
        return [
            DashboardStatsWidget::class,
        ];
    }

    protected function getForms(): array
    {
        return [
            'dashboardForm',
        ];
    }

    public function dashboardForm(Schema $schema): Schema
    {
        return $schema->components([
            // Recent Activity
            Section::make(__('Recent Activity'))
                ->icon('heroicon-o-clock')
                ->schema([
                    EmbeddedTable::make(RecentActivityTable::class),
                ]),
        ]);
    }

    protected function getHeaderActions(): array
    {
        return [
            Action::make('refresh')
                ->label(__('Refresh'))
                ->icon('heroicon-o-arrow-path')
                ->color('gray')
                ->action(fn () => $this->redirect(request()->url())),

            Action::make('onboarding')
                ->label(__('Setup Wizard'))
                ->icon('heroicon-o-sparkles')
                ->visible(fn () => ! DnsSetting::get('onboarding_completed', false))
                ->modalHeading(__('Welcome to Jabali!'))
                ->modalDescription(__('Let\'s get your server control panel set up.'))
                ->modalWidth('md')
                ->form([
                    TextInput::make('admin_email')
                        ->label(__('Your Email Address'))
                        ->helperText(__('Enter your email to receive important server notifications.'))
                        ->email()
                        ->placeholder('admin@example.com'),
                ])
                ->modalSubmitActionLabel(__('Get Started'))
                ->action(function (array $data): void {
                    if (! empty($data['admin_email'])) {
                        DnsSetting::set('admin_email_recipients', $data['admin_email']);
                    }
                    DnsSetting::set('onboarding_completed', '1');
                    DnsSetting::clearCache();
                }),
        ];
    }
}
