<?php

namespace App\Filament\Resources\AuditLogResource\Pages;

use App\Filament\Resources\AuditLogResource;
use App\Models\AuditLog;
use App\Services\AuditLog as AuditLogService;
use Filament\Infolists\Components\KeyValueEntry;
use Filament\Infolists\Components\TextEntry;
use Filament\Resources\Pages\ViewRecord;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Schema;

/**
 * Ported from AuditLogController::show().
 */
class ViewAuditLog extends ViewRecord
{
    protected static string $resource = AuditLogResource::class;

    public function infolist(Schema $schema): Schema
    {
        return $schema->components([
            Section::make()->schema([
                TextEntry::make('created_at')->label('When')->dateTime(),
                TextEntry::make('action')->badge(),
                TextEntry::make('actor.name')->label('Actor')->placeholder('System'),
                TextEntry::make('actor.email')->label('Email')->placeholder('—'),
                TextEntry::make('ip')->label('IP')->placeholder('—'),
                TextEntry::make('subject_type')->label('Subject type')->placeholder('—'),
                TextEntry::make('subject_id')->label('Subject ID')->placeholder('—'),
                TextEntry::make('description')
                    ->state(fn (AuditLog $record): string => $record->description ?: AuditLogService::describe($record))
                    ->columnSpanFull(),
            ])->columns(3),

            Section::make('Context')
                ->visible(fn (AuditLog $record): bool => filled($record->context))
                ->schema([
                    KeyValueEntry::make('context')->label('')->columnSpanFull(),
                ]),
        ]);
    }
}
