<?php

namespace App\Filament\Resources;

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

class StudentLifeEventsResource extends Resource
{
    protected static ?string $model = StudentLifeEvents::class;

    protected static ?string $navigationIcon = 'heroicon-o-calendar-days';
    protected static ?string $navigationLabel = 'Student Life Events';
    protected static ?string $pluralLabel = 'Student Life Events';
    protected static ?string $modelLabel = 'Student Life Event';
    protected static ?string $navigationGroup = 'Activities Content';
    protected static ?int $navigationSort = 2;

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

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

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

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

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

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

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

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                Forms\Components\Section::make('Event Information')
                    ->schema([
                        Forms\Components\TextInput::make('event_tittle')
                            ->label('Event Title')
                            ->required()
                            ->maxLength(255)
                            ->placeholder('Enter event title'),
                        Forms\Components\Textarea::make('event_description')
                            ->label('Event Description')
                            ->required()
                            ->maxLength(65535)
                            ->rows(4)
                            ->placeholder('Enter event description')
                            ->columnSpanFull(),
                    ])
                    ->columns(2),

                Forms\Components\Section::make('Event Media')
                    ->schema([
                        Forms\Components\FileUpload::make('event_images')
                            ->label('Event Images')
                            ->multiple()
                            ->image()
                            ->required()
                            ->disk('public')
                            ->directory('student-life-events')
                            ->visibility('public')
                            ->imageEditor()
                            ->placeholder('Upload event images')
                            ->helperText('You can upload multiple images for the event')
                            ->columnSpanFull(),
                        Forms\Components\ColorPicker::make('background_color')
                            ->label('Background Color')
                            ->required()
                            ->default('#ffffff')
                            ->helperText('Choose a background color for the event section'),
                    ])
                    ->columns(2),
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                Tables\Columns\TextColumn::make('event_tittle')
                    ->label('Event Title')
                    ->sortable()
                    ->searchable()
                    ->limit(50),
                Tables\Columns\TextColumn::make('event_description')
                    ->label('Description')
                    ->limit(100)
                    ->searchable()
                    ->toggleable(),
                Tables\Columns\TextColumn::make('event_images')
                    ->label('Images Count')
                    ->formatStateUsing(fn ($state) => is_array($state) ? count($state) : 0)
                    ->badge()
                    ->color('info'),
                Tables\Columns\ColorColumn::make('background_color')
                    ->label('Background Color')
                    ->toggleable(),
                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\Filter::make('has_images')
                    ->label('Has Images')
                    ->query(fn ($query) => $query->whereJsonLength('event_images', '>', 0)),
            ])
            ->actions([
                Tables\Actions\EditAction::make()
                    ->visible(fn () => Gate::allows('edit studentlifeevents')),
                Tables\Actions\DeleteAction::make()
                    ->visible(fn () => Gate::allows('delete studentlifeevents')),
            ])
            ->bulkActions([
                Tables\Actions\BulkActionGroup::make([
                    Tables\Actions\DeleteBulkAction::make()
                        ->visible(fn () => Gate::allows('delete studentlifeevents')),
                ]),
            ])
            ->defaultSort('created_at', 'desc');
    }

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

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