<?php

namespace App\Filament\Resources;

use App\Filament\Resources\ServicePhaseResource\Pages;
use App\Models\ServicePhase;
use Filament\Forms\Components\Checkbox;
use Filament\Forms\Components\Repeater;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\Tabs;
use Filament\Forms\Components\Tabs\Tab;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables\Actions\DeleteAction;
use Filament\Tables\Actions\EditAction;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;

class ServicePhaseResource extends Resource
{
    protected static ?string $model = ServicePhase::class;

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

    protected static ?string $navigationGroup = 'Contenu du site';

    protected static ?string $navigationLabel = 'Services (phases)';

    protected static ?string $modelLabel = 'phase de services';

    public static function form(Form $form): Form
    {
        return $form->schema([
            TextInput::make('svph_numero')->label('Numéro (01, 02…)')->required()->maxLength(10),
            TextInput::make('svph_ordre')->label('Ordre')->numeric()->default(0),
            Tabs::make('Traductions')->tabs([
                Tab::make('Français')->schema([
                    TextInput::make('svph_label_fr')->label('Intitulé de la phase')->required()->maxLength(255),
                ]),
                Tab::make('English')->schema([
                    TextInput::make('svph_label_en')->label('Phase label')->required()->maxLength(255),
                ]),
            ])->columnSpanFull(),

            Repeater::make('services')
                ->relationship('services')
                ->label('Services de cette phase')
                ->schema([
                    TextInput::make('svc_numero')->label('Numéro')->required()->maxLength(10),
                    Select::make('svc_icone')->label('Icône')->options([
                        'target' => 'Cible', 'graduation' => 'Formation', 'compass' => 'Boussole',
                        'star' => 'Étoile', 'home' => 'Maison', 'document-check' => 'Document validé',
                        'building' => 'Immeuble', 'network' => 'Réseau', 'globe' => 'Globe', 'lightbulb' => 'Ampoule',
                    ])->required(),
                    Checkbox::make('svc_exclusif')->label('Marquer comme "Exclusif"'),
                    TextInput::make('svc_ordre')->label('Ordre')->numeric()->default(0),
                    Tabs::make('Traductions')->tabs([
                        Tab::make('Français')->schema([
                            TextInput::make('svc_title_fr')->label('Titre')->required()->maxLength(255),
                            Textarea::make('svc_desc_fr')->label('Description')->required()->rows(2),
                        ]),
                        Tab::make('English')->schema([
                            TextInput::make('svc_title_en')->label('Title')->required()->maxLength(255),
                            Textarea::make('svc_desc_en')->label('Description')->required()->rows(2),
                        ]),
                    ])->columnSpanFull(),
                ])
                ->orderColumn('svc_ordre')
                ->collapsible()
                ->itemLabel(fn (array $state): ?string => $state['svc_title_fr'] ?? null)
                ->columnSpanFull(),
        ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->defaultSort('svph_ordre')
            ->reorderable('svph_ordre')
            ->columns([
                TextColumn::make('svph_numero')->label('N°'),
                TextColumn::make('svph_label_fr')->label('Phase (FR)'),
                TextColumn::make('services_count')->label('Services')->counts('services'),
            ])
            ->actions([
                EditAction::make()->modalWidth('4xl'),
                DeleteAction::make(),
            ]);
    }

    public static function getPages(): array
    {
        return ['index' => Pages\ListServicePhases::route('/')];
    }
}
