<?php

use MintyPHP\Buffer;
use MintyPHP\Domain\Taxonomy\UserLifecycleAction;
use MintyPHP\Http\SessionStoreInterface;
use MintyPHP\Router;
use MintyPHP\Service\Access\UiAccessService;
use MintyPHP\Service\Access\UiCapabilityMap;
use MintyPHP\Support\Flash;
use MintyPHP\Support\Guard;

$session = app(SessionStoreInterface::class)->all();
Guard::requireLogin();
Guard::requireAbility(\MintyPHP\Service\Access\OperationsAuthorizationPolicy::ABILITY_ADMIN_USER_LIFECYCLE_AUDIT_VIEW);

$auditId = (int) ($id ?? 0);
$auditService = app(\MintyPHP\Service\Audit\UserLifecycleAuditService::class);
$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'] ?? '') === UserLifecycleAction::Delete->value) {
    $snapshot = $auditService->decryptSnapshot($auditLog);
}
$viewAuth['page'] = app(UiAccessService::class)->pageCapabilities(
    (int) ($session['user']['id'] ?? 0),
    UiCapabilityMap::PAGE_USER_LIFECYCLE_VIEW
);

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