<?php

namespace App\Filament\Resources;

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

class JoinTeamSubmissionResource extends Resource
{
    protected static ?string $model = JoinTeamSubmission::class;

    protected static ?string $navigationIcon = 'heroicon-o-user-plus';
    protected static ?string $navigationLabel = 'Join Team Submissions';
    protected static ?string $pluralLabel = 'Join Team Submissions';
    protected static ?string $modelLabel = 'Join Team Submission';
    protected static ?string $navigationGroup = 'Form Content and Data';

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

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

    public static function canCreate(): bool
    {
        return false; // No create permission for submissions
    }

    public static function canEdit($record): bool
    {
        return false; // No edit permission for submissions
    }

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

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                Forms\Components\TextInput::make('name')->required()->maxLength(255),
                Forms\Components\TextInput::make('surname')->required()->maxLength(255),
                Forms\Components\TextInput::make('grade')->maxLength(255),
                Forms\Components\TextInput::make('section')->maxLength(255),
                Forms\Components\TextInput::make('phone_number')->maxLength(255),
                Forms\Components\TextInput::make('email')->email()->maxLength(255),
                Forms\Components\Textarea::make('message')->maxLength(65535),
                Forms\Components\Select::make('game_id')
                    ->relationship('game', 'main_title')
                    ->label('Game')
                    ->searchable()
                    ->preload(),
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                Tables\Columns\TextColumn::make('name')->sortable()->searchable(),
                Tables\Columns\TextColumn::make('surname')->sortable()->searchable(),
                Tables\Columns\TextColumn::make('email')->sortable()->searchable(),
                Tables\Columns\TextColumn::make('phone_number')->sortable(),
                Tables\Columns\TextColumn::make('game.main_title')->label('Game')->sortable()->searchable(),
                Tables\Columns\TextColumn::make('created_at')->dateTime()->sortable()->toggleable(isToggledHiddenByDefault: true),
            ])
            ->filters([
                Tables\Filters\SelectFilter::make('game_id')
                    ->relationship('game', 'main_title')
                    ->label('Filter by Game')
                    ->placeholder('All Games')
                    ->searchable()
                    ->preload()
                    ->indicateUsing(function (array $data): ?string {
                        if (! $data['value']) {
                            return null;
                        }

                        return 'Game: ' . \App\Models\Game::find($data['value'])?->main_title;
                    }),
            ])
            ->actions([
                Tables\Actions\ViewAction::make()
                    ->visible(fn () => Gate::allows('view jointteamsubmission')),
                Tables\Actions\DeleteAction::make()
                    ->visible(fn () => Gate::allows('delete jointteamsubmission')),
            ])
            ->bulkActions([
                Tables\Actions\BulkActionGroup::make([
                    Tables\Actions\DeleteBulkAction::make()
                        ->visible(fn () => Gate::allows('delete jointteamsubmission')),
                ]),
            ]);
    }

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

    public static function getPages(): array
    {
        return [
            'index' => Pages\ListJoinTeamSubmissions::route('/'),
            'view' => Pages\ViewJoinTeamSubmission::route('/{record}'),
        ];
    }
}
