<?php

declare(strict_types=1);

namespace App\Service\WorkHours;

final readonly class DailyReferenceMinutesResolver
{
    /**
     * Returns the contractual expected minutes for a given weekday.
     *
     * - Saturday/Sunday: always 0
     * - If $workDaysMinutes is provided (per-employee schedule on `EmployeeContractPeriod`),
     *   it takes precedence: returns the minutes for that iso day if scheduled, 0 otherwise.
     * - Else 35h: 7h every weekday
     * - Else 39h: 8h Mon-Thu, 7h Fri
     * - Else other positive values: weeklyHours/5 per weekday
     * - Else null/<=0 weeklyHours: 0
     *
     * @param int                  $isoWeekDay      1 = Monday ... 7 = Sunday
     * @param null|array<int, int> $workDaysMinutes iso-day → minutes (1=Mon, ..., 5=Fri)
     */
    public function resolve(?int $weeklyHours, int $isoWeekDay, ?array $workDaysMinutes = null): int
    {
        if ($isoWeekDay >= 6) {
            return 0;
        }

        if (null !== $workDaysMinutes) {
            return (int) ($workDaysMinutes[$isoWeekDay] ?? 0);
        }

        if (null === $weeklyHours || $weeklyHours <= 0) {
            return 0;
        }

        if (39 === $weeklyHours) {
            return 5 === $isoWeekDay ? 7 * 60 : 8 * 60;
        }

        if (35 === $weeklyHours) {
            return 7 * 60;
        }

        return (int) round(($weeklyHours * 60) / 5);
    }
}
