<?php

namespace App\Filament\Resources;

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

class BoardMemberResource extends Resource
{
    protected static ?string $model = BoardMember::class;

    protected static ?string $navigationIcon = 'heroicon-o-user-group';
    protected static ?string $navigationLabel = 'Board Members';
    protected static ?string $pluralLabel = 'Board Members';
    protected static ?string $modelLabel = 'Board Member';
    protected static ?string $navigationGroup = 'Academic Management';
    protected static ?int $navigationSort = 10;

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

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

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

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

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

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

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

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                Forms\Components\TextInput::make('name')
                    ->required()
                    ->maxLength(255)
                    ->label('Name'),
                Forms\Components\TextInput::make('position')
                    ->required()
                    ->maxLength(255)
                    ->label('Position'),
                Forms\Components\Textarea::make('description')
                    ->maxLength(65535)
                    ->label('Description'),
                Forms\Components\TextInput::make('college')
                    ->maxLength(255)
                    ->label('College'),
                Forms\Components\FileUpload::make('image')
                    ->image()
                    ->label('Image'),
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                Tables\Columns\TextColumn::make('name')->searchable()->sortable(),
                Tables\Columns\TextColumn::make('position')->searchable()->sortable(),
                Tables\Columns\TextColumn::make('description')->limit(50),
                Tables\Columns\TextColumn::make('college')->sortable(),
                Tables\Columns\ImageColumn::make('image')->label('Image'),
                Tables\Columns\TextColumn::make('created_at')->dateTime()->sortable(),
            ])
            ->filters([
                // Add filters if needed
            ])
            ->actions([
                Tables\Actions\EditAction::make()
                    ->visible(fn () => Gate::allows('edit boardmember')),
                Tables\Actions\DeleteAction::make()
                    ->visible(fn () => Gate::allows('delete boardmember')),
            ])
            ->bulkActions([
                Tables\Actions\BulkActionGroup::make([
                    Tables\Actions\DeleteBulkAction::make()
                        ->visible(fn () => Gate::allows('delete boardmember')),
                ]),
            ]);
    }

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