<?php

namespace App\Filament\Tenant\Resources\UserResource\RelationManagers;

use App\Auth\Permissions;
use Filament\Actions;
use Filament\Forms;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Schemas;
use Filament\Schemas\Schema;
use Filament\Tables;
use Filament\Tables\Table;
use Spatie\Permission\Models\Permission;

class PermissionOverridesRelationManager extends RelationManager
{
    protected static string $relationship = 'permissionOverrides';

    protected static ?string $title = 'Excepții drepturi';

    protected static string|\BackedEnum|null $icon = 'heroicon-o-shield-exclamation';

    public function form(Schema $schema): Schema
    {
        return $schema->components([
            Schemas\Components\Section::make('Excepție')
                ->columns(2)
                ->schema([
                    Forms\Components\Select::make('permission_id')
                        ->label('Drept')
                        ->required()
                        ->searchable()
                        ->options(fn () => Permission::orderBy('name')->pluck('name', 'id'))
                        ->columnSpanFull(),
                    Forms\Components\Select::make('mode')
                        ->required()
                        ->options(['grant' => 'GRANT — adaugă dreptul', 'deny' => 'DENY — interzice dreptul'])
                        ->default('grant'),
                    Forms\Components\DatePicker::make('expires_at')
                        ->label('Expiră la (opțional)')
                        ->minDate(now()),
                    Forms\Components\Textarea::make('reason')
                        ->label('Motiv')
                        ->columnSpanFull()
                        ->placeholder('Ex: lockdown temporar; acces pentru audit; etc.')
                        ->rows(2),
                ]),
        ]);
    }

    public function table(Table $table): Table
    {
        return $table
            ->recordTitleAttribute('mode')
            ->columns([
                Tables\Columns\TextColumn::make('permission.name')
                    ->label('Drept')
                    ->searchable()
                    ->sortable(),
                Tables\Columns\TextColumn::make('mode')
                    ->badge()
                    ->colors(['success' => 'grant', 'danger' => 'deny'])
                    ->formatStateUsing(fn ($state) => strtoupper($state)),
                Tables\Columns\TextColumn::make('reason')
                    ->limit(40)
                    ->placeholder('—'),
                Tables\Columns\TextColumn::make('expires_at')
                    ->label('Expiră')
                    ->date()
                    ->placeholder('niciodată')
                    ->color(fn ($record) => $record?->isExpired() ? 'danger' : 'gray'),
                Tables\Columns\TextColumn::make('grantedBy.name')
                    ->label('Acordat de')
                    ->placeholder('—')
                    ->toggleable(),
            ])
            ->headerActions([
                Actions\CreateAction::make()
                    ->mutateDataUsing(fn (array $data) => array_merge($data, [
                        'granted_at' => now(),
                        'granted_by_id' => auth()->id(),
                    ])),
            ])
            ->actions([
                Actions\EditAction::make(),
                Actions\DeleteAction::make(),
            ])
            ->defaultSort('granted_at', 'desc');
    }
}
