<?php

namespace App\Filament\Admin\Resources\Blogs\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\Textarea;
use Filament\Forms\Components\Toggle;
use Filament\Schemas\Components\Grid;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Schema;
use Illuminate\Support\Str;

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

                    /*
                |--------------------------------------------------------------------------
                | Blog Settings
                |--------------------------------------------------------------------------
                */
                    Section::make('Blog Settings')
                        ->description('Visibility and post options.')
                        ->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')
                                ->default(true)
                                ->helperText('Enable or disable the post.')
                                ->required(),

                            Toggle::make('is_featured')
                                ->label('Featured Post')
                                ->default(true)

                                ->helperText('Mark this post as featured.')
                                ->required(),
                        ])
                        ->columns(2)
                        ->columnSpanFull(),

                    /*
                |--------------------------------------------------------------------------
                | Titles & Content (Multilingual)
                |--------------------------------------------------------------------------
                */
                    Section::make('Titles & Content (Multilingual)')
                        ->description('Enter the title and content in both 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_title')
                                            ->label('English Title')
                                            ->placeholder('Enter English title')
                                            ->live(onBlur: true)
                                            ->afterStateUpdated(function ($state, callable $set) {
                                                if ($state) {
                                                    $set('slug', Str::slug($state, '-'));
                                                }
                                            }),

                                        RichEditor::make('en_description')
                                            ->label('English Description')
                                            ->placeholder('Write English content')
                                            ->columnSpanFull(),
                                    ])
                                    ->columnSpanFull(),

                                // Persian Section (RTL)
                                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('عنوان را وارد کنید')
                                            ->live(onBlur: true)
                                            ->afterStateUpdated(function ($state, callable $set) {
                                                if ($state) {
                                                    $set('slug', Str::slug($state, '-'));
                                                }
                                            }),

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

                            ]),

                        ])
                        ->columnSpanFull(),

                    /*
                |--------------------------------------------------------------------------
                | Thumbnail
                |--------------------------------------------------------------------------
                */
                    Section::make('Thumbnail')
                        ->extraAttributes([
                            'style' => 'background-color:#ffffff;padding:12px;border-radius:6px;border:1px solid #e5e7eb;'
                        ])
                        ->schema([
                            FileUpload::make('thumbnail')
                                ->label('Thumbnail')
                                ->image()
                                ->imageEditor()
                                ->disk('public')
                                ->directory('blogs')
                                ->columnSpanFull(),
                        ])
                        ->columnSpanFull(),

                    /*
                |--------------------------------------------------------------------------
                | Slug
                |--------------------------------------------------------------------------
                */
                    Section::make('Slug')
                        ->extraAttributes([
                            'style' => 'background-color:#ffffff;padding:12px;border-radius:6px;border:1px solid #e5e7eb;'
                        ])
                        ->schema([
                            TextInput::make('slug')
                                ->label('Slug')
                                ->placeholder('Auto-generated from title')
                                ->required()
                                ->readOnly()
                                ->unique(ignoreRecord: true)
                                ->columnSpanFull(),
                        ])
                        ->columnSpanFull(),

                    /*
                |--------------------------------------------------------------------------
                | Author
                |--------------------------------------------------------------------------
                */
                    Section::make('Author')
                        ->extraAttributes([
                            'style' => 'background-color:#ffffff;padding:12px;border-radius:6px;border:1px solid #e5e7eb;'
                        ])
                        ->schema([
                            Select::make('user_id')
                                ->label('Author')
                                ->relationship('user', 'name')
                                ->default(fn() => auth()->id())
                                ->disabled()
                                ->dehydrated()
                                ->required()
                                ->native(false),
                        ])
                        ->columnSpanFull(),

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