<?php

namespace App\Filament\Resources\Leads\Schemas;

use App\Models\ConnectorStage;
use App\Models\LeadStage;
use Filament\Forms\Components\DateTimePicker;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Textarea;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Components\Tabs;
use Filament\Schemas\Components\Tabs\Tab;
use Filament\Schemas\Schema;

class LeadForm
{
    public static function configure(Schema $schema): Schema
    {
        return $schema
            ->components([
                Tabs::make()
                    ->columnSpanFull()
                    ->tabs([
                        Tab::make('Contacto')
                            ->columns(2)
                            ->schema([
                                TextInput::make('first_name')->label('Nombre'),
                                TextInput::make('last_name')->label('Apellido'),
                                TextInput::make('company')->label('Empresa'),
                                TextInput::make('email')->label('Correo')->email(),
                                TextInput::make('phone')->label('Teléfono')->tel(),
                                TextInput::make('whatsapp')->label('WhatsApp')->tel(),
                                TextInput::make('linkedin')->label('LinkedIn')->url(),
                                TextInput::make('country')->label('País'),
                                TextInput::make('state')->label('Estado / Provincia'),
                                TextInput::make('city')->label('Ciudad'),
                            ]),

                        Tab::make('Clasificación')
                            ->columns(2)
                            ->schema([
                                // Catálogo editable desde Ajustes → Etapas de leads.
                                Select::make('status')
                                    ->label('Etapa')
                                    ->options(fn (): array => LeadStage::options())
                                    ->default('new')
                                    ->native(false),
                                Select::make('contact_type')
                                    ->label('Tipo de contacto')
                                    ->options([
                                        'lead' => 'Lead',
                                        'prospect' => 'Prospecto',
                                        'connector' => 'Conector',
                                    ])
                                    ->native(false),
                                Select::make('connector_stage')
                                    ->label('Etapa de conector')
                                    ->options(fn (): array => ConnectorStage::options())
                                    ->native(false)
                                    ->helperText('Solo aplica si el contacto es un conector.'),
                                Select::make('referred_by')
                                    ->label('Referido por')
                                    ->relationship('referrer', 'company')
                                    ->searchable()
                                    ->preload(),
                                TextInput::make('source')->label('Origen')->required(),
                                Select::make('client_id')
                                    ->label('Cliente')
                                    ->relationship('client', 'company_name')
                                    ->searchable()
                                    ->preload(),
                                TextInput::make('fit_score')
                                    ->label('Encaje (0-10)')
                                    ->numeric()->minValue(0)->maxValue(10),
                                TextInput::make('contact_ease')
                                    ->label('Facilidad de contacto (0-10)')
                                    ->numeric()->minValue(0)->maxValue(10),
                            ]),

                        Tab::make('Interés')
                            ->columns(2)
                            ->schema([
                                TextInput::make('product_interest')->label('Producto de interés'),
                                TextInput::make('quantity')->label('Cantidad'),
                                Textarea::make('message')->label('Mensaje')->rows(4)->columnSpanFull(),
                                Textarea::make('next_action')->label('Siguiente acción')->rows(2)->columnSpanFull(),
                            ]),

                        Tab::make('Empresa')
                            ->columns(2)
                            ->schema([
                                TextInput::make('company_website')->label('Sitio web')->url(),
                                TextInput::make('company_email')->label('Correo')->email(),
                                TextInput::make('company_phone')->label('Teléfono')->tel(),
                                TextInput::make('company_linkedin')->label('LinkedIn')->url(),
                                TextInput::make('company_facebook')->label('Facebook')->url(),
                                TextInput::make('company_instagram')->label('Instagram')->url(),
                                TextInput::make('company_youtube')->label('YouTube')->url(),
                                TextInput::make('company_tiktok')->label('TikTok')->url(),
                                TextInput::make('company_twitter')->label('X / Twitter')->url(),
                                TextInput::make('site_access')->label('Acceso al sitio'),
                            ]),

                        Tab::make('Prospección')
                            ->schema([
                                Select::make('outreach_status')
                                    ->label('Estado de prospección')
                                    ->options([
                                        'sin_prospectar' => 'Sin prospectar',
                                        'investigando' => 'Investigando',
                                        'borrador' => 'Borrador listo',
                                        'aprobado' => 'Aprobado',
                                        'enviado' => 'Enviado',
                                        'respondido' => 'Respondió',
                                        'descartado' => 'Descartado',
                                    ])
                                    ->default('sin_prospectar')
                                    ->required()
                                    ->native(false),
                                TextInput::make('outreach_subject')->label('Asunto'),
                                Textarea::make('outreach_draft')->label('Borrador del correo')->rows(8),
                                DateTimePicker::make('outreach_sent_at')->label('Enviado el'),
                                Textarea::make('research_note')->label('Nota de investigación')->rows(4),
                                Textarea::make('ai_comments')->label('Comentarios de la IA')->rows(4),
                            ]),

                        Tab::make('Origen web')
                            ->columns(2)
                            ->schema([
                                Section::make()
                                    ->columns(2)
                                    ->columnSpanFull()
                                    ->schema([
                                        TextInput::make('utm_source')->label('UTM source'),
                                        TextInput::make('utm_medium')->label('UTM medium'),
                                        TextInput::make('utm_campaign')->label('UTM campaign'),
                                        TextInput::make('utm_content')->label('UTM content'),
                                        TextInput::make('visitor_id')
                                            ->label('Visitor ID')
                                            ->disabled()
                                            ->helperText('Lo pone el sitio de Astro. Solo lectura.'),
                                    ]),
                            ]),
                    ]),
            ]);
    }
}
