<?php

declare(strict_types=1);

namespace Maestro\Core\Modules\HourRegistration\Services;

use Maestro\Core\Modules\HourRegistration\Models\HourTimesheetLine;

final class HourTotalsCalculator
{
    public function lineTotalHours(HourTimesheetLine $line): float
    {
        $sum = 0.0;
        foreach (HourTimesheetLine::WEEKDAY_KEYS as $day) {
            $sum += (float) $line->{'duration_'.$day};
        }

        return round($sum, 2);
    }

    /** @param  iterable<HourTimesheetLine>  $lines */
    public function dayTotals(iterable $lines): array
    {
        $out = [];
        foreach (HourTimesheetLine::WEEKDAY_KEYS as $d) {
            $out[$d] = 0.0;
        }

        foreach ($lines as $line) {
            foreach (HourTimesheetLine::WEEKDAY_KEYS as $d) {
                $out[$d] += (float) $line->{'duration_'.$d};
            }
        }

        foreach ($out as $k => $v) {
            $out[$k] = round($v, 2);
        }

        return $out;
    }
}
