<?php

namespace App\Filament\Clusters\RareAdmin\Resources\SuperAdminActionLogs;

use App\Filament\Clusters\RareAdmin\RareAdminCluster;
use App\Filament\Clusters\RareAdmin\Resources\SuperAdminActionLogs\Pages\ManageSuperAdminActionLogs;
use App\Models\SuperAdminActionLog;
use BackedEnum;
use Filament\Resources\Resource;
use Filament\Schemas\Schema;
use Filament\Support\Icons\Heroicon;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;
use UnitEnum;

class SuperAdminActionLogResource extends Resource
{
    protected static ?string $model = SuperAdminActionLog::class;

    protected static ?string $cluster = RareAdminCluster::class;

    protected static string|BackedEnum|null $navigationIcon = Heroicon::OutlinedClipboardDocumentList;

    protected static ?int $navigationSort = 95;

    protected static ?string $recordTitleAttribute = 'action';

    public static function getNavigationGroup(): UnitEnum|string|null
    {
        return __('admin.nav.infrastructure');
    }

    public static function getNavigationLabel(): string
    {
        return 'Audit log';
    }

    public static function canCreate(): bool
    {
        return false;
    }

    public static function canEdit($record): bool
    {
        return false;
    }

    public static function canDelete($record): bool
    {
        return false;
    }

    public static function canDeleteAny(): bool
    {
        return false;
    }

    public static function form(Schema $schema): Schema
    {
        return $schema
            ->components([
                //
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->recordTitleAttribute('action')
            ->columns([
                TextColumn::make('action')
                    ->badge()
                    ->searchable()
                    ->sortable(),
                TextColumn::make('actor.fullName')
                    ->label('Actor')
                    ->sortable()
                    ->searchable(),
                TextColumn::make('subject_type')
                    ->label('Subject')
                    ->formatStateUsing(fn (?string $state) => $state ? class_basename($state) : '—')
                    ->searchable()
                    ->toggleable(),
                TextColumn::make('subject_id')
                    ->label('Subject ID')
                    ->sortable()
                    ->toggleable(),
                TextColumn::make('metadata')
                    ->label('Fields')
                    ->formatStateUsing(function ($state): string {
                        if (! is_array($state)) {
                            return '—';
                        }

                        $fields = $state['fields'] ?? [];

                        return $fields ? implode(', ', $fields) : '—';
                    })
                    ->toggleable(isToggledHiddenByDefault: true)
                    ->wrap(),
                TextColumn::make('source')
                    ->label('Source')
                    ->limit(40)
                    ->toggleable(isToggledHiddenByDefault: true),
                TextColumn::make('occurred_at')
                    ->label('Timestamp')
                    ->dateTime()
                    ->sortable(),
            ])
            ->filters([
                //
            ])
            ->recordActions([])
            ->toolbarActions([]);
    }

    public static function getPages(): array
    {
        return [
            'index' => ManageSuperAdminActionLogs::route('/'),
        ];
    }
}
