<?php

namespace App\Filament\Resources;

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

class AcademicResource extends Resource
{
    protected static ?string $model = Academic::class;

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

    protected static ?string $navigationLabel = 'Academic';

    protected static ?int $navigationSort = 2;
    protected static ?string $navigationGroup = 'Academic Management';

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

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

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

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

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

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

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

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                Forms\Components\Section::make('Academic Information')
                    ->schema([
                        Forms\Components\TextInput::make('title')
                            ->required()
                            ->maxLength(255)
                            ->label('Academic Title'),

                        Forms\Components\TextInput::make('programs_count')
                            ->numeric()
                            ->required()
                            ->label('Number of Programs'),

                        Forms\Components\FileUpload::make('background_image')
                            ->directory('academic/backgrounds')
                            ->image()
                            ->label('Background Image'),
                    ])
                    ->columns(2),
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                Tables\Columns\TextColumn::make('title')
                    ->searchable()
                    ->sortable(),
                Tables\Columns\TextColumn::make('programs_count')
                    ->numeric()
                    ->sortable(),
                Tables\Columns\ImageColumn::make('background_image'),
                Tables\Columns\TextColumn::make('created_at')
                    ->dateTime()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
                Tables\Columns\TextColumn::make('updated_at')
                    ->dateTime()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
            ])
            ->filters([
                //
            ])
            ->actions([
                Tables\Actions\EditAction::make()
                    ->visible(fn () => Gate::allows('edit academic')),
                Tables\Actions\DeleteAction::make()
                    ->visible(fn () => Gate::allows('delete academic')),
            ])
            ->bulkActions([
                Tables\Actions\BulkActionGroup::make([
                    Tables\Actions\DeleteBulkAction::make()
                        ->visible(fn () => Gate::allows('delete academic')),
                ]),
            ]);
    }

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

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