<?php

namespace App\Filament\Resources;

use Filament\Forms;
use App\Models\Role;
use Filament\Tables;
use Filament\Forms\Form;
use Filament\Tables\Table;
use Filament\Resources\Resource;
use Filament\Forms\Components\Select;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Actions\ActionGroup;
use Illuminate\Database\Eloquent\Builder;
use App\Filament\Resources\RoleResource\Pages;
use Illuminate\Database\Eloquent\SoftDeletingScope;
use App\Filament\Resources\RoleResource\RelationManagers;
use Illuminate\Support\Facades\Gate;

class RoleResource extends Resource
{
    protected static ?string $model = Role::class;

    protected static ?string $navigationIcon = 'heroicon-o-finger-print';
    protected static ?int $navigationSort = 16;

    protected static ?string $navigationGroup = 'User Management';

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

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

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

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

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

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

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

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                Forms\Components\TextInput::make('name')
                    ->required()
                    ->maxLength(255),
                    Select::make('permissions')
                    ->multiple()
                    ->searchable()
                    ->preload()
                    ->relationship('permissions', 'name'),
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                TextColumn::make('name')
                    ->searchable()
                    ->sortable(),
                TextColumn::make('permissions.name')
                    ->label('Permissions')
                    ->badge(),
                TextColumn::make('created_at')
                    ->dateTime()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
                TextColumn::make('updated_at')
                    ->dateTime()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
            ])
            ->filters([
                Tables\Filters\TrashedFilter::make(),
            ])
            ->actions([
                Tables\Actions\EditAction::make()
                    ->visible(fn () => Gate::allows('edit role')),
                Tables\Actions\DeleteAction::make()
                    ->visible(fn () => Gate::allows('delete role')),
                Tables\Actions\ForceDeleteAction::make()
                    ->visible(fn () => Gate::allows('force delete role')),
                Tables\Actions\RestoreAction::make()
                    ->visible(fn () => Gate::allows('restore role')),
            ])
            ->bulkActions([
                Tables\Actions\BulkActionGroup::make([
                    Tables\Actions\DeleteBulkAction::make()
                        ->visible(fn () => Gate::allows('delete role')),
                    Tables\Actions\ForceDeleteBulkAction::make()
                        ->visible(fn () => Gate::allows('force delete role')),
                    Tables\Actions\RestoreBulkAction::make()
                        ->visible(fn () => Gate::allows('restore role')),
                ]),
            ]);
    }

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

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

    public static function getEloquentQuery(): Builder
    {
        return parent::getEloquentQuery()
            ->withoutGlobalScopes([
                SoftDeletingScope::class,
            ]);
    }
}
