<?php

namespace App\Filament\Admin\Resources\Packages\Schemas;

use Filament\Forms\Components\FileUpload;
use Filament\Forms\Components\RichEditor;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Toggle;
use Filament\Schemas\Components\Grid;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Schema;
use Illuminate\Support\Str;

class PackageForm
{
    public static function configure(Schema $schema): Schema
    {
        return $schema
            ->components([
                Grid::make()->schema([

                    /*
                    |----------------------------------------------------------------------
                    | Package Settings
                    |----------------------------------------------------------------------
                    */
                    Section::make('Package Settings')
                        ->description('Configure package visibility, featured status, and category.')
                        ->extraAttributes([
                            'style' => 'background-color:#ffffff;padding:15px;border-radius:8px;border:1px solid #e5e7eb;box-shadow:0 1px 3px rgba(0,0,0,0.05);'
                        ])
                        ->schema([
                            Toggle::make('is_active')
                                ->label('Active')
                                ->helperText('Enable or disable this package.')
                                ->required(),

                            Toggle::make('is_featured')
                                ->label('Featured')
                                ->helperText('Mark this package as featured.')
                                ->required(),
                        ])
                        ->columns(2)
                        ->columnSpanFull(),

                    Section::make('Relationship')->schema([
                        Select::make('package_category_id')
                            ->label('Package Category')
                            ->relationship('packageCategory', 'en_name')
                            ->searchable()
                            ->preload()
                            ->native(false)
                            ->required()
                            ->columnSpanFull(),
                    ])->columnSpanFull(),

                    /*
                    |----------------------------------------------------------------------
                    | Titles & Descriptions (Multilingual)
                    |----------------------------------------------------------------------
                    */
                    Section::make('Titles & Descriptions')
                        ->description('Enter the name and description in English and Persian.')
                        ->collapsible()
                        ->schema([
                            Grid::make()->schema([

                                // English Section
                                Section::make('English')
                                    ->collapsible()
                                    ->collapsed()
                                    ->extraAttributes([
                                        'style' => 'background-color:#f9fafb;padding:12px;border-radius:6px;border:1px solid #d1d5db;'
                                    ])
                                    ->schema([
                                        TextInput::make('en_name')
                                            ->label('English Name')
                                            ->placeholder('Enter package name in English')
                                            ->afterStateUpdated(function ($state, callable $set, callable $get) {
                                                $fa_name = $get('fa_name');
                                                $set('slug', Str::slug($state ?: $fa_name, '-'));
                                            }),

                                        RichEditor::make('en_description')
                                            ->label('English Description')
                                            ->placeholder('Enter package description in English')
                                            ->columnSpanFull(),
                                    ])
                                    ->columnSpanFull(),

                                // Persian Section
                                Section::make('فارسی')
                                    ->collapsible()
                                    ->collapsed()
                                    ->extraAttributes([
                                        'dir' => 'rtl',
                                        'class' => 'text-right',
                                        'style' => 'background-color:#f9fafb;padding:12px;border-radius:6px;border:1px solid #d1d5db;'
                                    ])
                                    ->schema([
                                        TextInput::make('fa_name')
                                            ->label('نام فارسی')
                                            ->extraAttributes(['dir' => 'rtl', 'class' => 'text-right'])
                                            ->placeholder('نام بسته به فارسی')
                                            ->afterStateUpdated(function ($state, callable $set, callable $get) {
                                                $en_name = $get('en_name');
                                                $set('slug', Str::slug($en_name ?: $state, '-'));
                                            }),

                                        RichEditor::make('fa_description')
                                            ->label('توضیحات فارسی')
                                            ->extraAttributes(['dir' => 'rtl', 'class' => 'text-right'])
                                            ->placeholder('توضیح بسته به فارسی')
                                            ->columnSpanFull(),
                                    ])
                                    ->columnSpanFull(),

                            ]),
                        ])
                        ->columnSpanFull(),

                    /*
                    |----------------------------------------------------------------------
                    | Thumbnail
                    |----------------------------------------------------------------------
                    */
                    Section::make('Thumbnail')
                        ->schema([
                            FileUpload::make('thumbnail')
                                ->label('Thumbnail')
                                ->image()
                                ->imageEditor()
                                ->multiple()
                                ->disk('public')
                                ->directory('packages')
                                ->columnSpanFull(),
                        ])
                        ->columnSpanFull(),

                    /*
                    |----------------------------------------------------------------------
                    | Pricing
                    |----------------------------------------------------------------------
                    */
                    Section::make('Pricing')
                        ->description('Set package price and discount.')
                        ->extraAttributes([
                            'style' => 'background-color:#ffffff;padding:12px;border-radius:6px;border:1px solid #e5e7eb;'
                        ])
                        ->schema([
                            TextInput::make('price')
                                ->label('Price')
                                ->numeric()
                                ->prefix('$')
                                ->required(),

                            TextInput::make('discount')
                                ->label('Discount')
                                ->numeric()
                                ->prefix('$')
                                ->required(),
                        ])
                        ->columns(2)
                        ->columnSpanFull(),

                    /*
                    |----------------------------------------------------------------------
                    | Slug
                    |----------------------------------------------------------------------
                    */
                    Section::make('Slug')
                        ->schema([
                            TextInput::make('slug')
                                ->label('Slug')
                                ->placeholder('Auto-generated from English or Persian name')
                                ->required()
                                ->readOnly()
                                ->columnSpanFull(),
                        ])
                        ->columnSpanFull(),

                ])->columnSpanFull(),
            ]);
    }
}
