<?php

namespace App\Filament\Resources;

use App\Models\NewEventContain;
use App\Models\IndividualZone;
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\FileUpload;
use Filament\Forms\Components\DateTimePicker;
use Filament\Forms\Components\Select;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Columns\ImageColumn;
use App\Filament\Resources\NewEventContainResource\Pages;
use Illuminate\Support\Facades\Gate;

class NewEventContainResource extends Resource
{
    protected static ?string $model = NewEventContain::class;

    protected static ?string $navigationIcon = 'heroicon-o-calendar';

    protected static ?string $navigationLabel = 'News & Events Content';

    protected static ?string $navigationGroup = 'Content Management';

    protected static ?int $navigationSort = 3;

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

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

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

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

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

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

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

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                Section::make('Content Information')
                    ->schema([
                        Select::make('new_event')
                            ->label('Type')
                            ->options([
                                'news' => 'News',
                                'events' => 'Events',
                            ])
                            ->required(),
                        Select::make('individual_zone_id')
                            ->label('Individual Zone')
                            ->options(function () {
                                $zones = IndividualZone::pluck('main_title', 'id')->toArray();
                                $zones['other'] = 'Others';
                                return $zones;
                            })
                            ->searchable()
                            ->reactive(),
                        Select::make('game_id')
                            ->label('Game')
                            ->options(function (callable $get) {
                                $zoneId = $get('individual_zone_id');
                                if ($zoneId && $zoneId !== 'other') {
                                    return \App\Models\Game::where('individual_zone_id', $zoneId)->pluck('main_title', 'id')->toArray();
                                }
                                return ['other' => 'Others'];
                            })
                            ->searchable()
                            ->nullable(),
                        FileUpload::make('image')
                            ->label('Event Image')
                            ->image()
                            ->imageResizeMode('cover')
                            ->imageCropAspectRatio('16:9')
                            ->imageResizeTargetWidth('1920')
                            ->imageResizeTargetHeight('1080')
                            ->directory('news-events')
                            ->required(),
                        TextInput::make('title')
                            ->label('Event Title')
                            ->required(),
                        DateTimePicker::make('released_at')
                            ->label('Release Date & Time')
                            ->required(),
                        Textarea::make('description')
                            ->label('Event Description')
                            ->required()
                            ->columnSpanFull(),
                        TextInput::make('author')
                            ->label('Author')
                            ->required(),
                    ])->columns(2),
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                TextColumn::make('new_event')
                    ->label('Type')
                    ->badge()
                    ->color(fn (string $state): string => match ($state) {
                        'news' => 'success',
                        'events' => 'warning',
                    }),
                ImageColumn::make('image')
                    ->label('Image')
                    ->circular(),
                TextColumn::make('title')
                    ->label('Title')
                    ->searchable()
                    ->sortable(),
                TextColumn::make('released_at')
                    ->label('Release Date')
                    ->dateTime()
                    ->sortable(),
                TextColumn::make('created_at')
                    ->dateTime()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
                TextColumn::make('updated_at')
                    ->dateTime()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
            ])
            ->filters([
                Tables\Filters\SelectFilter::make('new_event')
                    ->options([
                        'news' => 'News',
                        'events' => 'Events',
                    ]),
            ])
            ->actions([
                Tables\Actions\EditAction::make()
                    ->visible(fn () => Gate::allows('edit neweventcontain')),
                Tables\Actions\DeleteAction::make()
                    ->visible(fn () => Gate::allows('delete neweventcontain')),
            ])
            ->bulkActions([
                Tables\Actions\BulkActionGroup::make([
                    Tables\Actions\DeleteBulkAction::make()
                        ->visible(fn () => Gate::allows('delete neweventcontain')),
                ]),
            ]);
    }

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

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