<?php

namespace App\Modules\HR\Attendance\Services;

use App\Modules\Admin\Tenancy\Models\Tenant;
use App\Modules\Admin\Tenancy\Services\PlanLimitEnforcementService;
use App\Modules\Admin\Tenancy\Support\PlanLimitKeys;
use Carbon\CarbonImmutable;

class AttendanceHistoryWindowService
{
    public function __construct(
        private readonly PlanLimitEnforcementService $planLimits,
    ) {}

    public function earliestReportDate(Tenant $tenant): ?CarbonImmutable
    {
        $months = $this->planLimits->effectiveCap($tenant, PlanLimitKeys::ATTENDANCE_HISTORY_MONTHS);
        if ($months === null) {
            return null;
        }
        $m = max(1, (int) $months);

        return CarbonImmutable::now()->startOfMonth()->subMonthsNoOverflow($m - 1);
    }
}
