<?php

declare(strict_types=1);

namespace App\Filament\Resources\DberpNumberSequences\Schemas;

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 DberpNumberSequenceForm
{
    public static function configure(Schema $schema): Schema
    {
        return $schema
            ->components([
                Section::make(__('dberp.sections.number_rule'))
                    ->schema([
                        TextInput::make('code')
                            ->label(__('dberp.fields.rule_code'))
                            ->required()
                            ->maxLength(64)
                            ->unique(ignoreRecord: true)
                            ->disabled()
                            ->helperText(__('dberp.number_sequence_control.code_field_hint')),
                        TextInput::make('name')
                            ->label(__('dberp.fields.rule_name'))
                            ->required()
                            ->maxLength(255),
                        Select::make('applies_to')
                            ->label(__('dberp.fields.rule_applies_to'))
                            ->options(__('dberp.options.number_applies_to'))
                            ->required(),
                        TextInput::make('prefix')
                            ->label(__('dberp.fields.prefix'))
                            ->maxLength(32),
                        Select::make('date_segment')
                            ->label(__('dberp.fields.date_segment'))
                            ->options(__('dberp.options.date_segment'))
                            ->required(),
                        TextInput::make('serial_digits')
                            ->label(__('dberp.fields.serial_digits'))
                            ->numeric()
                            ->minValue(1)
                            ->maxValue(12)
                            ->required(),
                        TextInput::make('next_value')
                            ->label(__('dberp.fields.current_serial'))
                            ->numeric()
                            ->minValue(1)
                            ->required()
                            ->helperText(fn (): string => (string) __('dberp.hints.current_serial')."\n".(string) __('dberp.number_sequence_control.next_value_adjust_caution')),
                        Toggle::make('is_enabled')
                            ->label(__('dberp.fields.is_enabled'))
                            ->default(true)
                            ->helperText(__('dberp.number_sequence_control.is_enabled_hint')),
                        TextInput::make('pattern')
                            ->label(__('dberp.fields.pattern_optional'))
                            ->maxLength(512)
                            ->helperText(__('dberp.hints.pattern_optional')),
                        Textarea::make('remark')
                            ->label(__('dberp.fields.remark'))
                            ->rows(2)
                            ->columnSpanFull(),
                        TextInput::make('sort_order')
                            ->label(__('dberp.fields.sort_order'))
                            ->numeric()
                            ->default(0)
                            ->required(),
                    ])
                    ->columns(2),
            ]);
    }
}
