<?php

namespace App\Filament\Resources;

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

class ZoneResource extends Resource
{
    protected static ?string $model = Zone::class;

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

    protected static ?int $navigationSort = 5;
    protected static ?string $navigationGroup = 'Activities Content';

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                Forms\Components\TextInput::make('main_title')
                    ->required()
                    ->maxLength(255)
                    ->label('Zone Title'),
                Forms\Components\Textarea::make('text')
                    ->required()
                    ->maxLength(65535)
                    ->columnSpanFull()
                    ->label('Description'),
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                Tables\Columns\TextColumn::make('main_title')
                    ->sortable()
                    ->searchable()
                    ->label('Zone Title'),
                Tables\Columns\TextColumn::make('text')
                    ->limit(50)
                    ->searchable()
                    ->label('Description'),
                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 zone')),
                Tables\Actions\DeleteAction::make()->visible(fn () => Gate::allows('delete zone')),
            ])
            ->bulkActions([
                Tables\Actions\BulkActionGroup::make([
                    Tables\Actions\DeleteBulkAction::make()->visible(fn () => Gate::allows('delete zone')),
                ]),
            ]);
    }

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

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

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

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

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

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

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

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

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