<?php

namespace App\Filament\Resources;

use App\Models\AlumniMember;
use Filament\Forms;
use Filament\Tables;
use Filament\Forms\Form;
use Filament\Tables\Table;
use Filament\Resources\Resource;
use Filament\Forms\Components\Section;
use Filament\Forms\Components\Repeater;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\FileUpload;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Columns\ImageColumn;
use App\Filament\Resources\AlumniMemberResource\Pages;
use Illuminate\Support\Facades\Gate;

class AlumniMemberResource extends Resource
{
    protected static ?string $model = AlumniMember::class;
    protected static ?string $navigationIcon = 'heroicon-o-user-group';
    protected static ?string $navigationGroup = 'Website Content';

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

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

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

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

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

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

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

    public static function form(Form $form): Form
    {
        return $form->schema([
            Section::make('Alumni Member Details')->schema([
                TextInput::make('alumni_name')->label('Name')->required()->columnSpan(2),
                FileUpload::make('alumni_photo')->label('Photo')->image()->directory('alumni/members/photos')->required()->columnSpan(2),
                TextInput::make('company_name')->label('Company Name')->columnSpan(2),
                TextInput::make('alumni_position')->label('Position')->columnSpan(2),
                Textarea::make('alumni_description')->label('Description')->columnSpanFull(),
            ])->columns(2),
        ]);
    }

    public static function table(Table $table): Table
    {
        return $table->columns([
            TextColumn::make('alumni_name')->label('Name')->searchable()->sortable(),
            ImageColumn::make('alumni_photo')->label('Photo'),
            TextColumn::make('company_name')->label('Company Name')->searchable(),
            TextColumn::make('alumni_position')->label('Position')->searchable(),
            TextColumn::make('alumni_description')->label('Description')->limit(30),
            TextColumn::make('created_at')->dateTime()->sortable()->toggleable(isToggledHiddenByDefault: true),
            TextColumn::make('updated_at')->dateTime()->sortable()->toggleable(isToggledHiddenByDefault: true),
        ])
        ->actions([
            Tables\Actions\EditAction::make()
                ->visible(fn () => Gate::allows('edit alumnimember')),
            Tables\Actions\DeleteAction::make()
                ->visible(fn () => Gate::allows('delete alumnimember')),
        ])
        ->bulkActions([
            Tables\Actions\BulkActionGroup::make([
                Tables\Actions\DeleteBulkAction::make()
                    ->visible(fn () => Gate::allows('delete alumnimember')),
            ]),
        ]);
    }

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