<?php

namespace App\Filament\Resources;

use App\Models\RequestInfoSubmission;
use Filament\Tables;
use Filament\Tables\Table;
use Filament\Resources\Resource;
use Filament\Tables\Columns\TextColumn;
use App\Filament\Resources\RequestInfoSubmissionResource\Pages;
use Illuminate\Support\Facades\Gate;

class RequestInfoSubmissionResource extends Resource
{
    protected static ?string $model = RequestInfoSubmission::class;
    protected static ?string $navigationIcon = 'heroicon-o-envelope';
    protected static ?string $navigationGroup = 'Form Content and Data';

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

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

    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 requestinfosubmission');
    }

    public static function table(Table $table): Table
    {
        return $table->columns([
            TextColumn::make('firstName')->searchable()->sortable(),
            TextColumn::make('lastName')->searchable(),
            TextColumn::make('country')->searchable(),
            TextColumn::make('region')->searchable(),
            TextColumn::make('phone_number'),
            TextColumn::make('email')->searchable(),
            TextColumn::make('message')->limit(30),
            TextColumn::make('created_at')->dateTime()->sortable()->toggleable(isToggledHiddenByDefault: true),
            TextColumn::make('updated_at')->dateTime()->sortable()->toggleable(isToggledHiddenByDefault: true),
        ])
        ->actions([
            Tables\Actions\ViewAction::make()
                ->visible(fn () => Gate::allows('view requestinfosubmission')),
            Tables\Actions\DeleteAction::make()
                ->visible(fn () => Gate::allows('delete requestinfosubmission')),
        ])
        ->bulkActions([
            Tables\Actions\BulkActionGroup::make([
                Tables\Actions\DeleteBulkAction::make()
                    ->visible(fn () => Gate::allows('delete requestinfosubmission')),
            ]),
        ]);
    }

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