<?php

namespace App\Filament\Resources;

use App\Models\RequestInfo;
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\Tables\Columns\TextColumn;
use App\Filament\Resources\RequestInfoResource\Pages;
use Illuminate\Support\Facades\Gate;

class RequestInfoResource extends Resource
{
    protected static ?string $model = RequestInfo::class;
    protected static ?string $navigationIcon = 'heroicon-o-information-circle';
    protected static ?string $navigationGroup = 'Form Content and Data';

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

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

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

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

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

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

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

    public static function form(Form $form): Form
    {
        return $form->schema([
            Section::make('Request Info Details')->schema([
                TextInput::make('title')->required(),
                Textarea::make('text'),
                TextInput::make('location'),
                TextInput::make('longitude'),
                TextInput::make('latitude'),
                Forms\Components\Section::make('Button Settings')
                    ->schema([
                        Forms\Components\TextInput::make('button_label')->maxLength(255)->placeholder('Enter button text'),
                        Forms\Components\ColorPicker::make('button_color')->label('Button Text Color'),
                        Forms\Components\ColorPicker::make('button_background_color')->label('Button Background Color'),
                    ])
                    ->columns(3),
            ])->columns(2),
        ]);
    }

    public static function table(Table $table): Table
    {
        return $table->columns([
            TextColumn::make('title')->searchable()->sortable(),
            TextColumn::make('location')->searchable(),
            TextColumn::make('longitude'),
            TextColumn::make('latitude'),
            TextColumn::make('button_label'),
            TextColumn::make('button_color'),
            TextColumn::make('button_background_color'),
            TextColumn::make('created_at')->dateTime()->sortable()->toggleable(isToggledHiddenByDefault: true),
            TextColumn::make('updated_at')->dateTime()->sortable()->toggleable(isToggledHiddenByDefault: true),
        ])
        ->actions([
            Tables\Actions\EditAction::make()
                ->visible(fn () => Gate::allows('edit requestinfo')),
            Tables\Actions\DeleteAction::make()
                ->visible(fn () => Gate::allows('delete requestinfo')),
        ])
        ->bulkActions([
            Tables\Actions\BulkActionGroup::make([
                Tables\Actions\DeleteBulkAction::make()
                    ->visible(fn () => Gate::allows('delete requestinfo')),
            ]),
        ]);
    }

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