<?php

namespace App\Filament\Resources;

use App\Filament\Resources\AuditLogResource\Pages\ListAuditLogs;
use App\Models\AuditLog;
use BackedEnum;
use Filament\Resources\Resource;
use Filament\Tables\Table;
use Filament\Tables\Columns\TextColumn;
use Filament\Actions\ViewAction;
use Filament\Forms\Components\Placeholder;
use Filament\Schemas\Components\Section;

class AuditLogResource extends Resource
{
    protected static ?string $model = AuditLog::class;

    protected static string|BackedEnum|null $navigationIcon = 'heroicon-o-shield-check';

    protected static string|\UnitEnum|null $navigationGroup = 'Settings';

    protected static ?int $navigationSort = 5;

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                TextColumn::make('user.name')
                    ->label('User')
                    ->searchable()
                    ->sortable()
                    ->default('System'),
                TextColumn::make('event_type')
                    ->badge()
                    ->color(fn (string $state): string => match ($state) {
                        'settings_updated' => 'warning',
                        'login_failed' => 'danger',
                        'login_success' => 'success',
                        default => 'gray',
                    })
                    ->sortable(),
                TextColumn::make('description')
                    ->searchable()
                    ->wrap()
                    ->limit(50),
                TextColumn::make('ip_address')
                    ->label('IP Address')
                    ->searchable()
                    ->sortable(),
                TextColumn::make('created_at')
                    ->label('Logged At')
                    ->dateTime('M j, Y H:i:s')
                    ->sortable(),
                TextColumn::make('user_agent')
                    ->label('User Agent')
                    ->searchable()
                    ->wrap()
                    ->toggleable(isToggledHiddenByDefault: true),
            ])
            ->defaultSort('created_at', 'desc')
            ->actions([
                ViewAction::make()
                    ->modalWidth('xl')
                    ->form([
                        Section::make('Event Metadata')
                            ->schema([
                                Placeholder::make('_meta_row')
                                    ->hiddenLabel()
                                    ->columnSpanFull()
                                    ->view('filament.components.audit-log-modal'),
                            ]),

                        Section::make('Visual Changes Diff')
                            ->schema([
                                Placeholder::make('value_diff')
                                    ->hiddenLabel()
                                    ->view('filament.components.audit-log-diff')
                                    ->columnSpanFull(),
                            ])
                            ->collapsible(),
                    ])
            ])
            ->bulkActions([]);
    }

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