<?php

namespace App\Filament\Pages;

use BackedEnum;
use Filament\Forms\Components\Checkbox;
use Filament\Forms\Components\KeyValue;
use Filament\Forms\Components\Repeater;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Schema;
use Illuminate\Support\Str;
use Inerba\DbConfig\AbstractPageSettings;

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

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

    protected static string|BackedEnum|null $navigationIcon = 'heroicon-o-wrench-screwdriver'; // 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 = 'configuration-settings'; // Uncomment if you want to set a custom slug

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

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

    /**
     * Provide default values.
     *
     * @return array<string, mixed>
     */
    public function getDefaultData(): array
    {
        return [
            'enable_masking_external_link' => true,
            'enable_create_from_url' => true,
            'font_family' => [
                'head' => 'Poppins',
                'body' => 'Poppins',
            ],
            'default_language' => 'en',
            'fetch_seconds' => 15,
            'email_limit' => 10,
            'fetch_messages_limit' => 15,
            'cron_password' => Str::random(20),
            'date_format' => 'd M Y h:i A',
            'custom_username_length_min' => 3,
            'custom_username_length_max' => 20,
            'random_username_length_min' => 0,
            'random_username_length_max' => 0,
            'after_last_email_delete' => 'redirect_to_homepage',

        ];
    }

    public function form(Schema $schema): Schema
    {
        return $schema
            ->components([
                Section::make('General Configuration')
                    ->schema([
                        Checkbox::make('enable_masking_external_link')
                            ->label('Enable URL Masking of External URL')
                            ->default(false)
                            ->columnSpan(1)
                            ->helperText('If you enable this, then we will use href.li to remove your site footprint being passed-on to external link.'),
                        Checkbox::make('disable_mailbox_slug')
                            ->label('Disable Mailbox Slug')
                            ->default(false)
                            ->columnSpan(1)
                            ->helperText('If you enable this, then we will disable mailbox slug.'),
                        Checkbox::make('enable_create_from_url')
                            ->label('Enable Mail ID Creation from URL')
                            ->default(true)
                            ->columnSpan(1)
                            ->helperText('If you enable this, then users will be able to create email ID from URL.'),
                        Checkbox::make('enable_ad_block_detector')
                            ->label('Enable Ad Block Detector')
                            ->default(true)
                            ->columnSpan(1)
                            ->helperText('If you enable this, then we block all the users from using when that have Ad Blocker enabled.'),
                        KeyValue::make('font_family')
                            ->label('Font Family')
                            ->columnSpan(2)
                            ->helperText('Use Google Fonts with exact name.'),
                    ]),

                Select::make('default_language')->options([
                    'ar' => 'Arabic',
                    'de' => 'German',
                    'en' => 'English',
                    'fr' => 'French',
                    'hi' => 'Hindi',
                    'pl' => 'Polish',
                    'ru' => 'Russian',
                    'es' => 'Spanish',
                    'vi' => 'Viet',
                    'tr' => 'Turkish',
                    'no' => 'Norwegian',
                    'id' => 'Indonesian',
                ]),

                Section::make('Mailbox Configuration')
                    ->collapsed()
                    ->schema([
                        Checkbox::make('add_mail_in_title')->label('Add Mail In Title')->default(false)->required()->helperText('If you enable this, then we will automatically add the created temp mail to the page title.'),
                        Checkbox::make('disable_used_email')->label('Disable Used Email')->default(false)->required()->helperText('If you enable this, same email address cannot be created by user from different IP for one week.'),
                        TextInput::make('fetch_seconds')->label('Fetch Seconds')->numeric()->required(),
                        TextInput::make('email_limit')->label('Email Limit')->numeric()->required()->helperText('Limit on number of email ids that can be created per IP address in 24 hours. Recommended - 5.'),
                        TextInput::make('fetch_messages_limit')->label('Fetch Messages Limit')->numeric()->required()->helperText('Limit of messages retrieved at a time. Keep it to as low as possible. Default - 15.'),
                        TextInput::make('cron_password')->label('Cron Password')->required(),
                        TextInput::make('date_format')->label('Date Format')->placeholder('d M Y h:i A')->required(),
                        Section::make('Custom Username Lengths')
                            ->description('Username length for custom input')
                            ->columns(2)
                            ->schema([
                                TextInput::make('custom_username_length_min')->numeric()->minValue(3)->label('Custom Username Min Length')->required(),
                                TextInput::make('custom_username_length_max')->numeric()->minValue(3)->label('Custom Username Max Length')->required(),
                            ]),
                        Section::make('Random Username Lengths')
                            ->description('Username length for random username')
                            ->columns(2)
                            ->schema([
                                TextInput::make('random_username_length_min')->numeric()->minValue(0)->label('Random Username Min Length')->required(),
                                TextInput::make('random_username_length_max')->numeric()->minValue(0)->label('Random Username Max Length')->required(),
                            ]),
                        Select::make('after_last_email_delete')->options([
                            'redirect_to_homepage' => 'Redirect to homepage',
                            'create_new_email' => 'Create new Email',
                        ])->required(),
                    ]),
                Section::make('Forbidden Username & Blocked Domains')
                    ->columns(2)
                    ->collapsed()
                    ->schema([
                        Repeater::make('forbidden_ids')
                            ->statePath('forbidden_ids')
                            ->columnSpan(1)
                            ->schema([
                                TextInput::make('forbidden_id')->label('Forbidden IDs')->required()->maxLength(30),
                            ]),
                        Repeater::make('blocked_domains')
                            ->statePath('blocked_domains')
                            ->columnSpan(1)
                            ->schema([
                                TextInput::make('blocked_domain')->label('Blocked Domain')->required()->maxLength(30),
                            ]),
                    ]),
            ])
            ->statePath('data');
    }
}
