<?php

namespace App\Filament\Resources;

use App\Filament\Resources\SemesterResource\Pages;
use App\Models\Semester;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Support\Facades\Gate;

class SemesterResource extends Resource
{
    protected static ?string $model = Semester::class;

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

    protected static ?string $navigationGroup = 'Academic Management';

    protected static ?int $navigationSort = 4;

    public static function canViewAny(): bool
    {
        return Gate::allows('view semester');
    }

    public static function canView($record): bool
    {
        return Gate::allows('view semester');
    }

    public static function canCreate(): bool
    {
        return Gate::allows('create semester');
    }

    public static function canEdit($record): bool
    {
        return Gate::allows('edit semester');
    }

    public static function canDelete($record): bool
    {
        return Gate::allows('delete semester');
    }

    public static function canRestore($record): bool
    {
        return Gate::allows('restore semester');
    }

    public static function canForceDelete($record): bool
    {
        return Gate::allows('force delete semester');
    }

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                Forms\Components\Select::make('program_id')
                    ->relationship('program', 'main_title')
                    ->required()
                    ->searchable()
                    ->preload()
                    ->label('Program'),
                Forms\Components\TextInput::make('name')
                    ->required()
                    ->maxLength(255)
                    ->label('Semester Name')
                    ->placeholder('e.g., Semester 1, Semester 2'),
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                Tables\Columns\TextColumn::make('program.main_title')
                    ->sortable()
                    ->searchable()
                    ->label('Program'),
                Tables\Columns\TextColumn::make('name')
                    ->sortable()
                    ->searchable()
                    ->label('Semester Name'),
                Tables\Columns\TextColumn::make('created_at')
                    ->dateTime()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
                Tables\Columns\TextColumn::make('updated_at')
                    ->dateTime()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
            ])
            ->filters([
                Tables\Filters\SelectFilter::make('program')
                    ->relationship('program', 'main_title')
                    ->label('Filter by Program'),
            ])
            ->actions([
                Tables\Actions\EditAction::make()
                    ->visible(fn () => Gate::allows('edit semester')),
                Tables\Actions\DeleteAction::make()
                    ->visible(fn () => Gate::allows('delete semester')),
            ])
            ->bulkActions([
                Tables\Actions\BulkActionGroup::make([
                    Tables\Actions\DeleteBulkAction::make()
                        ->visible(fn () => Gate::allows('delete semester')),
                ]),
            ]);
    }

    public static function getRelations(): array
    {
        return [
            //
        ];
    }

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