<?php

namespace App\Filament\Resources;

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

class GameResource extends Resource
{
    protected static ?string $model = Game::class;

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

    protected static ?int $navigationSort = 7;
    protected static ?string $navigationLabel = 'Game/Zone/Section';
    protected static ?string $pluralLabel = 'Game/Zone/Section';
    protected static ?string $modelLabel = 'Game/Zone/Section';
    protected static ?string $navigationGroup = 'Activities Content';

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

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

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

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

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

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

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

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                Forms\Components\TextInput::make('main_title')
                    ->required()
                    ->maxLength(255)
                    ->label('Main Title'),
                Forms\Components\Textarea::make('description')
                    ->maxLength(65535)
                    ->columnSpanFull()
                    ->label('Description'),
                Forms\Components\ColorPicker::make('background_color')
                    ->label('Background Color'),
                Forms\Components\FileUpload::make('icon')
                    ->image()
                    ->label('Icon'),
                Forms\Components\FileUpload::make('images')
                    ->multiple()
                    ->image()
                    ->label('Images'),
                Forms\Components\TextInput::make('secondary_title')
                    ->maxLength(255)
                    ->label('Secondary Title'),
                Forms\Components\TextInput::make('event_tittle')
                    ->maxLength(255)
                    ->label('Event Title'),
                Forms\Components\TextInput::make('max_event_count')
                    ->numeric()
                    ->label('Max Event Count'),
                Forms\Components\TextInput::make('join_title')
                    ->maxLength(255)
                    ->label('Join Title'),
                Forms\Components\TextInput::make('button_text')
                    ->maxLength(255)
                    ->label('Button Text'),
                Forms\Components\ColorPicker::make('button_color')
                    ->label('Button Color'),
                Forms\Components\ColorPicker::make('button_background_color')
                    ->label('Button Background Color'),
                Forms\Components\Select::make('individual_zone_id')
                    ->relationship('individualZone', 'main_title')
                    ->label('Individual Zone'),
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                Tables\Columns\TextColumn::make('main_title')
                    ->sortable()
                    ->searchable()
                    ->label('Main Title'),
                Tables\Columns\TextColumn::make('secondary_title')
                    ->sortable()
                    ->searchable()
                    ->label('Secondary Title'),
                Tables\Columns\TextColumn::make('event_tittle')
                    ->label('Event Title'),
                Tables\Columns\TextColumn::make('max_event_count')
                    ->numeric()
                    ->sortable()
                    ->label('Max Event Count'),
                Tables\Columns\TextColumn::make('individualZone.main_title')
                    ->sortable()
                    ->searchable()
                    ->label('Individual Zone'),
                Tables\Columns\TextColumn::make('background_color')
                    ->label('Background Color'),
                Tables\Columns\ImageColumn::make('icon')
                    ->label('Icon'),
                Tables\Columns\TextColumn::make('button_text')
                    ->label('Button Text'),
                Tables\Columns\TextColumn::make('button_color')
                    ->label('Button Color'),
                Tables\Columns\TextColumn::make('button_background_color')
                    ->label('Button Background Color'),
                Tables\Columns\TextColumn::make('created_at')
                    ->dateTime()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
                Tables\Columns\TextColumn::make('updated_at')
                    ->dateTime()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
            ])
            ->filters([
                //
            ])
            ->actions([
                Tables\Actions\EditAction::make()
                    ->visible(fn () => Gate::allows('edit game')),
                Tables\Actions\DeleteAction::make()
                    ->visible(fn () => Gate::allows('delete game')),
            ])
            ->bulkActions([
                Tables\Actions\BulkActionGroup::make([
                    Tables\Actions\DeleteBulkAction::make()
                        ->visible(fn () => Gate::allows('delete game')),
                ]),
            ]);
    }

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

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