<?php

declare(strict_types=1);

namespace App\Service\WorkHours;

use App\Entity\EmployeeRttPayment;

/**
 * Convertit les paiements RTT (stockés par exercice Juin N-1 -> Mai N + mois)
 * en agrégats par ANNEE CIVILE (Janv-Déc). Heures payées = base25 + base50,
 * hors majoration (bonus). Plafond : 350 h chauffeur, 220 h autres.
 */
final readonly class OvertimePaidContingentCalculator
{
    public const int CAP_HOURS_DRIVER  = 350;
    public const int CAP_HOURS_DEFAULT = 220;

    /**
     * @param iterable<EmployeeRttPayment> $payments paiements d'un employé
     *                                               (typiquement exercices civilYear et civilYear+1)
     *
     * @return array<int, int> clé 1..12 -> minutes base payées (base25+base50)
     */
    public function monthlyBaseMinutes(iterable $payments, int $civilYear): array
    {
        $months = array_fill(1, 12, 0);

        foreach ($payments as $payment) {
            $month            = $payment->getMonth();
            $paymentCivilYear = $month >= 6 ? $payment->getYear() - 1 : $payment->getYear();
            if ($paymentCivilYear !== $civilYear) {
                continue;
            }

            $months[$month] += $payment->getBase25Minutes() + $payment->getBase50Minutes();
        }

        return $months;
    }

    /**
     * @param iterable<EmployeeRttPayment> $payments
     */
    public function totalBaseMinutes(iterable $payments, int $civilYear): int
    {
        return array_sum($this->monthlyBaseMinutes($payments, $civilYear));
    }

    public function capHours(bool $isDriver): int
    {
        return $isDriver ? self::CAP_HOURS_DRIVER : self::CAP_HOURS_DEFAULT;
    }
}
