<?php

namespace App\Filament\Resources\Quotes\Schemas;

use App\Models\DocumentType;
use Filament\Forms\Components\DatePicker;
use Filament\Forms\Components\KeyValue;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Textarea;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Schema;

class QuoteForm
{
    public static function configure(Schema $schema): Schema
    {
        return $schema
            ->components([
                Section::make('Documento')
                    ->columns(2)
                    ->schema([
                        TextInput::make('quote_number')
                            ->label('Folio')
                            ->required(),
                        // El tipo sale del catálogo document_types, editable
                        // desde Ajustes (prefijo, campos y bloques del PDF).
                        Select::make('document_type_id')
                            ->label('Tipo de documento')
                            ->options(fn (): array => DocumentType::query()
                                ->where('is_active', true)
                                ->pluck('label', 'id')
                                ->all())
                            ->native(false),
                        TextInput::make('type')
                            ->label('Tipo (texto)')
                            ->required()
                            ->helperText('Se conserva por compatibilidad con los datos migrados.'),
                        // Vocabulario fijo a propósito: 'accepted' dispara
                        // crear el proyecto. No es configurable.
                        Select::make('status')
                            ->label('Estado')
                            ->options([
                                'draft' => 'Borrador',
                                'sent' => 'Enviada',
                                'accepted' => 'Aceptada',
                                'rejected' => 'Rechazada',
                                'expired' => 'Vencida',
                            ])
                            ->default('draft')
                            ->native(false),
                        DatePicker::make('issue_date')
                            ->label('Fecha de emisión')
                            ->required(),
                        DatePicker::make('valid_until')
                            ->label('Válida hasta'),
                    ]),

                Section::make('Relaciones')
                    ->columns(2)
                    ->schema([
                        Select::make('client_id')
                            ->label('Cliente')
                            ->relationship('client', 'company_name')
                            ->searchable()
                            ->preload()
                            ->required(),
                        Select::make('deal_id')
                            ->label('Negocio')
                            ->relationship('deal', 'title')
                            ->searchable()
                            ->preload(),
                    ]),

                Section::make('Importes')
                    ->columns(3)
                    ->schema([
                        TextInput::make('subtotal')->label('Subtotal')->numeric()->default(0)->prefix('$'),
                        TextInput::make('tax_rate')->label('Tasa de impuesto')->numeric()->default(0)->suffix('%'),
                        TextInput::make('tax_amount')->label('Impuesto')->numeric()->default(0)->prefix('$'),
                        TextInput::make('total')->label('Total')->numeric()->default(0)->prefix('$'),
                        TextInput::make('currency')->label('Moneda')->default('USD'),
                        TextInput::make('position')->label('Posición')->numeric()->default(0),
                    ]),

                Section::make('Contenido')
                    ->schema([
                        Textarea::make('notes')->label('Notas')->rows(3),
                        Textarea::make('terms')->label('Términos')->rows(3),
                        TextInput::make('pdf_url')->label('URL del PDF')->url(),
                        KeyValue::make('custom_fields')
                            ->label('Campos personalizados')
                            ->keyLabel('Campo')
                            ->valueLabel('Valor'),
                    ]),
            ]);
    }
}
