<?php

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

use Filament\Forms\Components\FileUpload;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\Toggle;
use Filament\Schemas\Components\Grid;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Schema;

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

                    /*
                |--------------------------------------------------------------------------
                | Blog Settings
                |--------------------------------------------------------------------------
                */
                    Section::make('Blog Settings')
                        ->description('Enable or disable blog status and featured mode.')
                        ->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 the blog post.')
                                ->required(),

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

                    /*
                |--------------------------------------------------------------------------
                | Titles & Content (Multilingual)
                |--------------------------------------------------------------------------
                */
                    Section::make('Titles & Content')
                        ->description('Enter English and Persian titles and content.')
                        ->collapsible()
                        ->schema([

                            Grid::make()->schema([

                                // English
                                Section::make('English')
                                    ->collapsible()
                                    ->collapsed()
                                    ->extraAttributes([
                                        'style' =>
                                        'background-color:#f9fafb;padding:12px;border-radius:6px;
                                         border:1px solid #d1d5db;'
                                    ])
                                    ->schema([
                                        TextInput::make('en_title')
                                            ->label('English Title')
                                            ->placeholder('Enter English title')
                                            ->afterStateUpdated(function ($state, callable $set, callable $get) {
                                                $fa = $get('fa_title');
                                                $set('slug', \Illuminate\Support\Str::slug($state ?: $fa, '-'));
                                            }),

                                        Textarea::make('en_content')
                                            ->label('English Content')
                                            ->placeholder('Write English content here')
                                            ->columnSpanFull(),
                                    ])
                                    ->columnSpanFull(),

                                // Persian
                                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_title')
                                            ->label('عنوان فارسی')
                                            ->extraAttributes(['dir' => 'rtl', 'class' => 'text-right'])
                                            ->placeholder('عنوان فارسی را وارد کنید')
                                            ->afterStateUpdated(function ($state, callable $set, callable $get) {
                                                $en = $get('en_title');
                                                $set('slug', \Illuminate\Support\Str::slug($en ?: $state, '-'));
                                            }),

                                        Textarea::make('fa_content')
                                            ->label('محتوای فارسی')
                                            ->extraAttributes(['dir' => 'rtl', 'class' => 'text-right'])
                                            ->placeholder('متن فارسی را وارد کنید')
                                            ->columnSpanFull(),
                                    ])
                                    ->columnSpanFull(),

                            ]),

                        ])
                        ->columnSpanFull(),

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

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

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