<?php

namespace App\Filament\Resources\LegalDocuments\Schemas;

use App\Enums\LegalDocumentType;
use Filament\Forms\Components\DateTimePicker;
use Filament\Forms\Components\Repeater;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Toggle;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Schema;

class LegalDocumentForm
{
    public static function configure(Schema $schema): Schema
    {
        return $schema
            ->components([
                Section::make(__('admin.legal_documents.sections.identity'))
                    ->columns(3)
                    ->schema([
                        TextInput::make('slug')
                            ->label(__('admin.legal_documents.fields.slug'))
                            ->required()
                            ->maxLength(255)
                            ->helperText(__('admin.legal_documents.helpers.slug')),
                        Select::make('type')
                            ->label(__('admin.legal_documents.fields.type'))
                            ->options(LegalDocumentType::class)
                            ->required(),
                        TextInput::make('version')
                            ->label(__('admin.legal_documents.fields.version'))
                            ->integer()
                            ->minValue(1)
                            ->default(1)
                            ->required(),
                    ]),
                Section::make(__('admin.legal_documents.sections.publication'))
                    ->columns(2)
                    ->schema([
                        Toggle::make('is_active')
                            ->label(__('admin.legal_documents.fields.is_active'))
                            ->helperText(__('admin.legal_documents.helpers.is_active')),
                        DateTimePicker::make('published_at')
                            ->label(__('admin.legal_documents.fields.published_at'))
                            ->seconds(false)
                            ->helperText(__('admin.legal_documents.helpers.published_at')),
                    ]),
                Section::make(__('admin.legal_documents.sections.content'))
                    ->schema([
                        TextInput::make('title.fr')
                            ->label(__('admin.legal_documents.fields.title'))
                            ->default('')
                            ->maxLength(255)
                            ->required()
                            ->columnSpanFull(),
                        Repeater::make('content.fr.intro')
                            ->label(__('admin.legal_documents.fields.intro'))
                            ->simple(
                                Textarea::make('paragraph')
                                    ->label(__('admin.legal_documents.fields.paragraph'))
                                    ->rows(3)
                                    ->required()
                            )
                            ->default([
                                __('admin.legal_documents.defaults.intro'),
                            ])
                            ->addActionLabel(__('admin.legal_documents.actions.add_intro_paragraph'))
                            ->reorderable()
                            ->required()
                            ->columnSpanFull(),
                        Repeater::make('content.fr.sections')
                            ->label(__('admin.legal_documents.fields.sections'))
                            ->schema([
                                TextInput::make('title')
                                    ->label(__('admin.legal_documents.fields.section_title'))
                                    ->default(__('admin.legal_documents.defaults.section_title'))
                                    ->required()
                                    ->maxLength(255),
                                Repeater::make('body')
                                    ->label(__('admin.legal_documents.fields.section_body'))
                                    ->simple(
                                        Textarea::make('paragraph')
                                            ->label(__('admin.legal_documents.fields.paragraph'))
                                            ->rows(3)
                                            ->required()
                                    )
                                    ->default([
                                        __('admin.legal_documents.defaults.paragraph'),
                                    ])
                                    ->addActionLabel(__('admin.legal_documents.actions.add_section_paragraph'))
                                    ->reorderable()
                                    ->required()
                                    ->columnSpanFull(),
                            ])
                            ->default([
                                [
                                    'title' => __('admin.legal_documents.defaults.section_title'),
                                    'body' => [
                                        __('admin.legal_documents.defaults.paragraph'),
                                    ],
                                ],
                            ])
                            ->itemLabel(fn (array $state): ?string => $state['title'] ?? null)
                            ->addActionLabel(__('admin.legal_documents.actions.add_section'))
                            ->collapsible()
                            ->reorderable()
                            ->required()
                            ->columnSpanFull(),
                    ]),
            ]);
    }
}
