<?php

namespace MintyPHP\Module\Audit\Service;

use MintyPHP\Module\Audit\Domain\UserLifecycleAction;
use MintyPHP\Module\Audit\Domain\UserLifecycleStatus;
use MintyPHP\Module\Audit\Repository\UserLifecycleAuditRepository;
use MintyPHP\Service\Audit\UserLifecycleAuditDashboardInterface;

/**
 * Read-side dashboard implementation that delegates to {@see UserLifecycleAuditRepository}
 * and normalizes every input via the lifecycle taxonomy enums (defence-in-depth).
 */
final class UserLifecycleAuditDashboardService implements UserLifecycleAuditDashboardInterface
{
    public function __construct(private readonly UserLifecycleAuditRepository $userLifecycleAuditRepository)
    {
    }

    public function actionCountInWindow(string $action, int $days, string $status = 'success'): int
    {
        $normalizedAction = UserLifecycleAction::tryNormalize($action);
        if ($normalizedAction === null) {
            return 0;
        }
        $normalizedStatus = UserLifecycleStatus::tryNormalize($status);
        if ($normalizedStatus === null) {
            return 0;
        }
        return $this->userLifecycleAuditRepository->countActionInWindow(
            $normalizedAction->value,
            $days,
            $normalizedStatus->value
        );
    }

    public function summaryByAction(int $days, string $status = 'success'): array
    {
        $normalizedStatus = UserLifecycleStatus::tryNormalize($status);
        if ($normalizedStatus === null) {
            return [];
        }
        return $this->userLifecycleAuditRepository->sumByActionInWindow($days, $normalizedStatus->value);
    }
}
