<?php

namespace App\Filament\Resources;

use App\Filament\Resources\TransferResource\Pages;
use App\Models\Clasa;
use App\Models\Elev;
use App\Models\Transfer;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Support\Facades\Auth;

class TransferResource extends Resource
{
    protected static ?string $model = Transfer::class;
    protected static ?string $navigationIcon = 'heroicon-o-arrows-right-left';
    protected static ?string $navigationLabel = 'Transferuri';
    protected static ?string $pluralModelLabel = 'Transferuri';
    protected static ?string $modelLabel = 'Transfer';
    protected static ?string $navigationGroup = 'Administrare';

    public static function canAccess(): bool
    {
        $user = Auth::user();
        return $user && $user->hasAnyRole(['admin', 'director', 'secretariat']);
    }

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                Forms\Components\Select::make('elev_id')
                    ->label('Elev')
                    ->options(fn () => Elev::all()->map(fn ($e) => ['id' => $e->id, 'label' => $e->nume . ' ' . $e->prenume . ' (' . ($e->clasa?->nume ?? '') . ')'])->pluck('label', 'id'))
                    ->required()
                    ->searchable(),
                Forms\Components\Select::make('tip')
                    ->label('Tip transfer')
                    ->options([
                        'intrare' => 'Intrare (venit la noi)',
                        'iesire' => 'Ieșire (plecat de la noi)',
                    ])
                    ->required()
                    ->live(),
                Forms\Components\Select::make('clasa_veche_id')
                    ->label('Clasa veche')
                    ->options(fn () => Clasa::all()->pluck('nume', 'id'))
                    ->searchable()
                    ->nullable(),
                Forms\Components\Select::make('clasa_noua_id')
                    ->label('Clasa nouă')
                    ->options(fn () => Clasa::all()->pluck('nume', 'id'))
                    ->searchable()
                    ->nullable(),
                Forms\Components\TextInput::make('scoala_origine')
                    ->label('Școala de origine')
                    ->nullable(),
                Forms\Components\TextInput::make('scoala_destinatie')
                    ->label('Școala de destinație')
                    ->nullable(),
                Forms\Components\DatePicker::make('data_transfer')
                    ->label('Data transferului')
                    ->required()
                    ->default(now()),
                Forms\Components\TextInput::make('numar_document')
                    ->label('Număr document')
                    ->nullable(),
                Forms\Components\Textarea::make('observatii')
                    ->label('Observații')
                    ->nullable()
                    ->rows(3),
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                Tables\Columns\TextColumn::make('elev.nume')
                    ->label('Elev')
                    ->formatStateUsing(fn ($record) => $record->elev?->nume . ' ' . $record->elev?->prenume)
                    ->searchable(),
                Tables\Columns\TextColumn::make('tip')
                    ->label('Tip')
                    ->badge()
                    ->color(fn (string $state) => $state === 'intrare' ? 'success' : 'danger'),
                Tables\Columns\TextColumn::make('clasaVeche.nume')
                    ->label('Din clasa')
                    ->placeholder('—'),
                Tables\Columns\TextColumn::make('clasaNoua.nume')
                    ->label('În clasa')
                    ->placeholder('—'),
                Tables\Columns\TextColumn::make('data_transfer')
                    ->label('Data')
                    ->date('d.m.Y')
                    ->sortable(),
                Tables\Columns\TextColumn::make('numar_document')
                    ->label('Nr. document')
                    ->placeholder('—'),
            ])
            ->defaultSort('data_transfer', 'desc')
            ->actions([
                Tables\Actions\ViewAction::make(),
                Tables\Actions\EditAction::make(),
                Tables\Actions\DeleteAction::make(),
            ])
            ->bulkActions([]);
    }

    public static function mutateFormDataBeforeCreate(array $data): array
    {
        $data['inregistrat_de'] = Auth::id();
        return $data;
    }

    public static function getPages(): array
    {
        return [
            'index' => Pages\ListTransfers::route('/'),
            'create' => Pages\CreateTransfer::route('/create'),
            'edit' => Pages\EditTransfer::route('/{record}/edit'),
        ];
    }
}
