<?php

use MintyPHP\Buffer;
use MintyPHP\Router;
use MintyPHP\Support\Flash;
use MintyPHP\Support\Guard;
use MintyPHP\Service\Access\PermissionService;
use MintyPHP\Service\Audit\AuditServicesFactory;

Guard::requireLogin();
Guard::requirePermission(PermissionService::USER_LIFECYCLE_AUDIT_VIEW);

$auditId = (int) ($id ?? 0);
$factory = new AuditServicesFactory();
$auditService = $factory->createUserLifecycleAuditService();
$auditLog = $auditId > 0 ? $auditService->find($auditId) : null;
if (!$auditLog) {
    Flash::error(t('Lifecycle audit entry not found'), 'admin/user-lifecycle-audit', 'user_lifecycle_audit_not_found');
    Router::redirect('admin/user-lifecycle-audit');
}

$snapshot = null;
if ((string) ($auditLog['action'] ?? '') === 'delete') {
    $snapshot = $auditService->decryptSnapshot($auditLog);
}

Buffer::set('title', t('View user lifecycle audit entry'));
