<?php

namespace App\Filament\Resources;

use App\Filament\Resources\RegistruResource\Pages;
use App\Models\RegistruModificare;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Support\Facades\Auth;

class RegistruResource extends Resource
{
    protected static ?string $model = RegistruModificare::class;
    protected static ?string $navigationIcon = 'heroicon-o-clipboard-document-list';
    protected static ?string $navigationLabel = 'Registru modificări';
    protected static ?string $pluralModelLabel = 'Registru modificări';
    protected static ?string $modelLabel = 'Înregistrare';
    protected static ?string $navigationGroup = 'Administrare';
    protected static ?int $navigationSort = 99;

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

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

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                Tables\Columns\TextColumn::make('data_ora')
                    ->label('Data și ora')
                    ->dateTime('d.m.Y H:i:s')
                    ->sortable(),
                Tables\Columns\TextColumn::make('user_name')
                    ->label('Utilizator')
                    ->searchable(),
                Tables\Columns\TextColumn::make('actiune')
                    ->label('Acțiune')
                    ->badge()
                    ->color(fn (string $state): string => match ($state) {
                        'creat' => 'success',
                        'modificat' => 'warning',
                        'șters' => 'danger',
                        default => 'gray',
                    }),
                Tables\Columns\TextColumn::make('model')
                    ->label('Tip')
                    ->searchable(),
                Tables\Columns\TextColumn::make('descriere')
                    ->label('Descriere')
                    ->searchable()
                    ->wrap()
                    ->limit(80),
            ])
            ->defaultSort('data_ora', 'desc')
            ->filters([
                Tables\Filters\SelectFilter::make('actiune')
                    ->label('Acțiune')
                    ->options([
                        'creat' => 'Creat',
                        'modificat' => 'Modificat',
                        'șters' => 'Șters',
                    ]),
                Tables\Filters\SelectFilter::make('model')
                    ->label('Tip')
                    ->options([
                        'Nota' => 'Notă',
                        'Absenta' => 'Absență',
                        'Elev' => 'Elev',
                        'Clasa' => 'Clasă',
                        'Materie' => 'Materie',
                        'Perioada' => 'Perioadă',
                        'OrarSlot' => 'Orar',
                    ]),
            ])
            ->actions([
                Tables\Actions\ViewAction::make(),
            ])
            ->bulkActions([]);
    }

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