<?php

namespace App\Filament\Resources;

use App\Models\Alumni;
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\TextInput;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\Repeater;
use Filament\Forms\Components\FileUpload;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Columns\ImageColumn;
use App\Filament\Resources\AlumniResource\Pages;
use Illuminate\Support\Facades\Gate;

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

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

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

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

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

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

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

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

    public static function form(Form $form): Form
    {
        return $form->schema([
            Section::make('Alumni Main Section')->schema([
                TextInput::make('main_title')->required(),
                Textarea::make('main_description')->required(),
                FileUpload::make('background_image')
                    ->image()
                    ->directory('alumni/backgrounds')
                    ->label('Background Image')
                    ->required(),
                Repeater::make('images')
                    ->label('Gallery Images')
                    ->schema([
                        FileUpload::make('image')
                            ->image()
                            ->acceptedFileTypes(['image/*', 'image/gif'])
                            ->directory('alumni/images')
                            ->label('Image or GIF')
                            ->required(),
                    ])
                    ->addActionLabel('Add Image')
                    ->minItems(1)
                    ->maxItems(10)
                    ->columns(1),
            ])->columns(1),
            Section::make('Alumni Details')->schema([
                TextInput::make('second_title'),
                Textarea::make('second_description'),
                TextInput::make('success_title'),
                TextInput::make('alumni_members_count')->numeric()->label('Alumni Members Count'),
            ])->columns(1),
        ]);
    }

    public static function table(Table $table): Table
    {
        return $table->columns([
            TextColumn::make('main_title')->searchable()->sortable(),
            TextColumn::make('main_description')->limit(30),
            ImageColumn::make('background_image')->label('Background Image'),
            TextColumn::make('second_title')->searchable(),
            TextColumn::make('second_description')->limit(30),
            TextColumn::make('success_title')->searchable(),
            TextColumn::make('alumni_members_count')->label('Alumni Members Count'),
            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 alumni')),
            Tables\Actions\DeleteAction::make()
                ->visible(fn () => Gate::allows('delete alumni')),
        ])
        ->bulkActions([
            Tables\Actions\BulkActionGroup::make([
                Tables\Actions\DeleteBulkAction::make()
                    ->visible(fn () => Gate::allows('delete alumni')),
            ]),
        ]);
    }

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