<?php

namespace App\Filament\Resources;

use App\Filament\Resources\LeadResource\Pages;
use App\Models\Lead;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Infolists\Components\TextEntry;
use Filament\Infolists\Components\Section;
use Filament\Infolists\Infolist;
use Filament\Resources\Resource;
use Filament\Tables\Table;
use Filament\Tables;

class LeadResource extends Resource
{
    protected static ?string $model = Lead::class;

    protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';

    public static function form(Form $form): Form
    {
        return $form->schema([
            Forms\Components\TextInput::make('full_name')
                ->label('Nume complet')
                ->required()
                ->maxLength(255),
            Forms\Components\TextInput::make('phone')
                ->label('Telefon')
                ->required()
                ->maxLength(20),
            Forms\Components\TextInput::make('cod_fiscal')
                ->label('Cod fiscal')
                ->required()
                ->rule('digits:13')
                ->maxLength(13),
            Forms\Components\TextInput::make('email')
                ->label('Email')
                ->email()
                ->maxLength(255),
            Forms\Components\Textarea::make('message')
                ->label('Detalii')
                ->rows(4),
        ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                Tables\Columns\TextColumn::make('full_name')
                    ->label('Nume')
                    ->searchable()
                    ->sortable(),
                Tables\Columns\TextColumn::make('phone')
                    ->label('Telefon')
                    ->searchable(),
                Tables\Columns\TextColumn::make('cod_fiscal')
                    ->label('Cod fiscal')
                    ->searchable(),
                Tables\Columns\TextColumn::make('email')
                    ->label('Email')
                    ->searchable(),
                Tables\Columns\TextColumn::make('created_at')
                    ->label('Data')
                    ->dateTime('d.m.Y H:i')
                    ->sortable(),
            ])
            ->defaultSort('created_at', 'desc')
            ->actions([
                Tables\Actions\ViewAction::make(),
                Tables\Actions\EditAction::make(),
                Tables\Actions\DeleteAction::make(),
            ])
            ->bulkActions([
                Tables\Actions\DeleteBulkAction::make(),
            ]);
    }

    public static function infolist(Infolist $infolist): Infolist
    {
        return $infolist->schema([
            Section::make('Detalii lead')
                ->columns(2)
                ->schema([
                    TextEntry::make('full_name')
                        ->label('Nume complet')
                        ->icon('heroicon-o-user')
                        ->copyable(),
                    TextEntry::make('phone')
                        ->label('Telefon')
                        ->icon('heroicon-o-phone')
                        ->copyable(),
                    TextEntry::make('cod_fiscal')
                        ->label('Cod fiscal')
                        ->icon('heroicon-o-hashtag')
                        ->copyable(),
                    TextEntry::make('email')
                        ->label('Email')
                        ->icon('heroicon-o-envelope')
                        ->copyable()
                        ->placeholder('—'),
                    TextEntry::make('created_at')
                        ->label('Data')
                        ->icon('heroicon-o-calendar-days')
                        ->dateTime('d.m.Y H:i'),
                    TextEntry::make('message')
                        ->label('Detalii')
                        ->icon('heroicon-o-chat-bubble-left-right')
                        ->placeholder('—')
                        ->columnSpanFull(),
                ]),
        ]);
    }

    public static function getPages(): array
    {
        return [
            'index' => Pages\ListLeads::route('/'),
            'view' => Pages\ViewLead::route('/{record}'),
            'edit' => Pages\EditLead::route('/{record}/edit'),
        ];
    }
}
