<?php

namespace MintyPHP\Service\User;

use MintyPHP\Repository\User\UserLifecyclePolicyDashboardRepositoryInterface;

/**
 * Computes the core-domain KPIs for the user lifecycle settings dashboard.
 *
 * "Pending deletion" reads the users table directly. "Last run" reads the
 * settings table where UserLifecycleService::run() records every invocation
 * (success, failed, no-op alike) — independent of the audit module, so the
 * tile works even when audit is disabled.
 *
 * @api
 */
final class UserLifecyclePolicyDashboardService
{
    public function __construct(
        private readonly UserLifecyclePolicyDashboardRepositoryInterface $userLifecyclePolicyDashboardRepository,
        private readonly UserSettingsGateway $userSettingsGateway
    ) {
    }

    public function pendingDeletionCount(int $deactivateDays, int $deleteDays, int $windowDays = 7): int
    {
        if ($deactivateDays <= 0 || $deleteDays <= 0 || $windowDays <= 0) {
            return 0;
        }
        return $this->userLifecyclePolicyDashboardRepository->countPendingDeletion($deleteDays, $windowDays);
    }

    /**
     * @return array{created_at: string, status: string}|null
     *         null when no run has been recorded yet.
     */
    public function lastRun(): ?array
    {
        return $this->userSettingsGateway->getLifecycleLastRun();
    }
}
