<?php

namespace App\Filament\Resources;

use App\Filament\Resources\CorigentaResource\Pages;
use App\Models\Corigenta;
use App\Models\User;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Support\Facades\Auth;

class CorigentaResource extends Resource
{
    protected static ?string $model = Corigenta::class;
    protected static ?string $navigationIcon = 'heroicon-o-exclamation-triangle';
    protected static ?string $navigationLabel = 'Corigențe';
    protected static ?string $pluralModelLabel = 'Corigențe';
    protected static ?string $modelLabel = 'Corigență';
    protected static ?string $navigationGroup = 'Catalog';

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

    public static function canCreate(): bool
    {
        return false;
    }

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                Forms\Components\Select::make('profesor_id')
                    ->label('Profesor examinator')
                    ->options(fn () => User::role(['profesor', 'diriginte'])->pluck('name', 'id'))
                    ->searchable()
                    ->nullable(),
                Forms\Components\DatePicker::make('data_examen')
                    ->label('Data examenului'),
                Forms\Components\TextInput::make('nota_corigenta')
                    ->label('Nota la corigență')
                    ->numeric()
                    ->minValue(1)
                    ->maxValue(10)
                    ->nullable(),
                Forms\Components\Select::make('status')
                    ->label('Status')
                    ->options([
                        'programat' => 'Programat',
                        'promovat' => 'Promovat',
                        'nepromovat' => 'Nepromovat',
                    ])
                    ->required(),
                Forms\Components\Textarea::make('observatii')
                    ->label('Observații')
                    ->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('materie.nume')
                    ->label('Materie')
                    ->searchable(),
                Tables\Columns\TextColumn::make('perioada.nume')
                    ->label('Perioada'),
                Tables\Columns\TextColumn::make('media_inainte')
                    ->label('Media')
                    ->color('danger'),
                Tables\Columns\TextColumn::make('nota_corigenta')
                    ->label('Nota corig.')
                    ->placeholder('—'),
                Tables\Columns\TextColumn::make('data_examen')
                    ->label('Data examen')
                    ->date('d.m.Y')
                    ->placeholder('Neprogramat'),
                Tables\Columns\TextColumn::make('status')
                    ->label('Status')
                    ->badge()
                    ->color(fn (string $state) => match ($state) {
                        'programat' => 'warning',
                        'promovat' => 'success',
                        'nepromovat' => 'danger',
                        default => 'gray',
                    })
                    ->formatStateUsing(fn (string $state) => match ($state) {
                        'programat' => 'Programat',
                        'promovat' => 'Promovat',
                        'nepromovat' => 'Nepromovat',
                        default => $state,
                    }),
            ])
            ->defaultSort('created_at', 'desc')
            ->filters([
                Tables\Filters\SelectFilter::make('status')
                    ->options([
                        'programat' => 'Programat',
                        'promovat' => 'Promovat',
                        'nepromovat' => 'Nepromovat',
                    ]),
            ])
            ->actions([
                Tables\Actions\EditAction::make()
                    ->visible(fn () => Auth::user()?->hasAnyRole(['admin', 'director', 'secretariat'])),
            ])
            ->bulkActions([]);
    }

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