<?php

namespace App\Filament\Resources\Clients\Schemas;

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 ClientForm
{
    public static function configure(Schema $schema): Schema
    {
        return $schema
            ->components([
                Section::make('Cliente')
                    ->columns(2)
                    ->schema([
                        TextInput::make('company_name')
                            ->label('Empresa')
                            ->required()
                            ->columnSpanFull(),
                        TextInput::make('contact_name')->label('Contacto'),
                        TextInput::make('email')->label('Correo')->email(),
                        TextInput::make('phone')->label('Teléfono')->tel(),
                        TextInput::make('whatsapp')->label('WhatsApp')->tel(),
                        TextInput::make('country')->label('País'),
                        Select::make('status')
                            ->label('Estado')
                            ->options([
                                'active' => 'Activo',
                                'inactive' => 'Inactivo',
                            ])
                            ->default('active')
                            ->native(false),
                        Select::make('lead_id')
                            ->label('Lead de origen')
                            ->relationship('lead', 'company')
                            ->searchable()
                            ->preload(),
                    ]),

                Section::make('Facturación')
                    ->schema([
                        Textarea::make('billing_address')->label('Dirección de facturación')->rows(3),
                        TextInput::make('payment_terms')->label('Términos de pago'),
                        // default_fields es jsonb con cast 'array'.
                        KeyValue::make('default_fields')
                            ->label('Campos por defecto en documentos')
                            ->keyLabel('Campo')
                            ->valueLabel('Valor'),
                    ]),

                Section::make('Notas')
                    ->schema([
                        Textarea::make('notes')->label('Notas')->rows(4),
                    ]),
            ]);
    }
}
