<?php

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

use Filament\Forms\Components\FileUpload;
use Filament\Forms\Components\RichEditor;
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 ProductCategoryForm
{
    public static function configure(Schema $schema): Schema
    {
        return $schema
            ->components([
                Grid::make()->schema([

                    /*
                |------------------------------------------------------------------
                | Category Settings
                |------------------------------------------------------------------
                */
                    Section::make('Category Settings')
                        ->description('Configure category visibility and basic information.')
                        ->schema([
                            Toggle::make('is_active')
                                ->label('Active')
                                ->helperText('Enable or disable this category.')
                                ->required(),
                        ])
                        ->columnSpanFull(),

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

                                /*
                            |-------------------------
                            | English
                            |-------------------------
                            */
                                Section::make('English')
                                    ->collapsible()
                                    ->collapsed()
                                    ->schema([
                                        TextInput::make('en_name')
                                            ->label('English Name')
                                            ->placeholder('Enter category name in English')
                                            ->live(onBlur: true)
                                            ->afterStateUpdated(function ($state, $set, $get) {

                                                if (blank($state)) {
                                                    // English empty → fallback to Persian
                                                    $fa = $get('fa_name');
                                                    if (!blank($fa)) {
                                                        $set('slug', Str::slug($fa));
                                                    }
                                                    return;
                                                }

                                                $set('slug', Str::slug($state));
                                            }),

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

                                /*
                            |-------------------------
                            | Persian
                            |-------------------------
                            */
                                Section::make('فارسی')
                                    ->collapsible()
                                    ->collapsed()
                                    ->extraAttributes([
                                        'dir' => 'rtl',
                                        'class' => 'text-right',
                                    ])
                                    ->schema([
                                        TextInput::make('fa_name')
                                            ->label('نام فارسی')
                                            ->extraAttributes(['dir' => 'rtl', 'class' => 'text-right'])
                                            ->placeholder('نام دسته‌بندی به فارسی')
                                            ->live(onBlur: true)
                                            ->afterStateUpdated(function ($state, $set, $get) {

                                                // Only use Persian if English is empty
                                                if (!blank($get('en_name'))) {
                                                    return;
                                                }

                                                if (!blank($state)) {
                                                    $set('slug', Str::slug($state));
                                                }
                                            }),

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

                            ]),
                        ])
                        ->columnSpanFull(),

                    /*
                |------------------------------------------------------------------
                | Media
                |------------------------------------------------------------------
                */
                    Section::make('Media')
                        ->schema([
                            FileUpload::make('media')
                                ->label('Media')
                                ->image()
                                ->imageEditor()
                                ->disk('public')
                                ->directory('products')
                                ->columnSpanFull(),

                            FileUpload::make('thumbnail')
                                ->label('Thumbnail')
                                ->image()
                                ->imageEditor()
                                ->disk('public')
                                ->directory('products')
                                ->columnSpanFull(),
                        ])
                        ->columnSpanFull(),

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

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