<?php

namespace App\Filament\Resources;

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

class StudentLifeResource extends Resource
{
    protected static ?string $model = StudentLife::class;

    protected static ?string $navigationIcon = 'heroicon-o-sparkles';
    protected static ?string $navigationLabel = 'Student Life';
    protected static ?string $pluralLabel = 'Student Life';
    protected static ?string $modelLabel = 'Student Life';
    protected static ?string $navigationGroup = 'Activities Content';
    protected static ?int $navigationSort = 1;

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

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

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

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

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

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

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

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                Forms\Components\Section::make('Basic Information')
                    ->schema([
                        Forms\Components\TextInput::make('main_title')
                            ->label('Main Title')
                            ->maxLength(255)
                            ->placeholder('Enter the main title for student life section'),
                        Forms\Components\Textarea::make('text')
                            ->label('Short Description')
                            ->maxLength(65535)
                            ->rows(3)
                            ->placeholder('Enter a brief description')
                            ->helperText('A concise summary of the student life section'),
                        Forms\Components\Textarea::make('description')
                            ->label('Detailed Description')
                            ->maxLength(65535)
                            ->rows(6)
                            ->placeholder('Enter the detailed description')
                            ->helperText('A comprehensive description of student life activities')
                            ->columnSpanFull(),
                    ])
                    ->columns(2),

                Forms\Components\Section::make('Media Content')
                    ->schema([
                        Forms\Components\FileUpload::make('background_image')
                            ->label('Background Image')
                            ->image()
                            ->disk('public')
                            ->directory('student-life/backgrounds')
                            ->visibility('public')
                            ->imageEditor()
                            ->placeholder('Upload background image'),

                        Forms\Components\FileUpload::make('images')
                            ->label('Gallery Images')
                            ->multiple()
                            ->image()
                            ->disk('public')
                            ->directory('student-life/gallery')
                            ->visibility('public')
                            ->imageEditor()
                            ->placeholder('Upload multiple gallery images')
                            ->helperText('You can upload multiple images for the gallery section')
                            ->columnSpanFull(),
                    ])
                    ->columns(2),

                Forms\Components\Section::make('Statistics')
                    ->schema([
                        Forms\Components\TextInput::make('event_count')
                            ->label('Event Count')
                            ->numeric()
                            ->minValue(0)
                            ->placeholder('Enter the number of events')
                            ->helperText('This will be displayed as a statistic'),
                        Forms\Components\TextInput::make('images_count')
                            ->label('Images Count')
                            ->numeric()
                            ->minValue(0)
                            ->placeholder('Enter the number of images')
                            ->helperText('This will be displayed as a statistic'),
                    ])
                    ->columns(2),
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                Tables\Columns\TextColumn::make('main_title')
                    ->label('Main Title')
                    ->sortable()
                    ->searchable()
                    ->limit(50),
                Tables\Columns\TextColumn::make('text')
                    ->label('Short Description')
                    ->limit(80)
                    ->searchable()
                    ->toggleable(),
                Tables\Columns\TextColumn::make('description')
                    ->label('Detailed Description')
                    ->limit(100)
                    ->searchable()
                    ->toggleable(),
                Tables\Columns\TextColumn::make('event_count')
                    ->label('Event Count')
                    ->sortable()
                    ->badge()
                    ->color('success'),
                Tables\Columns\TextColumn::make('images_count')
                    ->label('Images Count')
                    ->sortable()
                    ->badge()
                    ->color('info'),
                Tables\Columns\ImageColumn::make('background_image')
                    ->label('Background Image')
                    ->circular()
                    ->size(40),
                Tables\Columns\TextColumn::make('images')
                    ->label('Gallery Images')
                    ->formatStateUsing(fn ($state) => is_array($state) ? count($state) : 0)
                    ->badge()
                    ->color('warning'),
                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_events')
                    ->label('Has Events')
                    ->query(fn ($query) => $query->where('event_count', '>', 0)),
                Tables\Filters\Filter::make('has_images')
                    ->label('Has Images')
                    ->query(fn ($query) => $query->where('images_count', '>', 0)),
                Tables\Filters\Filter::make('has_gallery_images')
                    ->label('Has Gallery Images')
                    ->query(fn ($query) => $query->whereJsonLength('images', '>', 0)),
            ])
            ->actions([
                Tables\Actions\EditAction::make()
                    ->visible(fn () => Gate::allows('edit studentlife')),
                Tables\Actions\DeleteAction::make()
                    ->visible(fn () => Gate::allows('delete studentlife')),
            ])
            ->bulkActions([
                Tables\Actions\BulkActionGroup::make([
                    Tables\Actions\DeleteBulkAction::make()
                        ->visible(fn () => Gate::allows('delete studentlife')),
                ]),
            ])
            ->defaultSort('created_at', 'desc');
    }

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

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