<?php

declare(strict_types=1);

namespace Maestro\Core\Modules\HourRegistration\Services;

use Illuminate\Support\Collection;
use Maestro\Core\Modules\HourRegistration\Models\HourTimesheetLine;

final class HourWeekSubmissionWarningsService
{
    public function __construct(
        private readonly HourTotalsCalculator $totalsCalculator,
    ) {}

    /** @param  Collection<int, HourTimesheetLine>  $lines */
    public function collect(Collection $lines): array
    {
        $warnings = [];
        $dayTotals = $this->totalsCalculator->dayTotals($lines);

        foreach (HourTimesheetLine::WEEKDAY_KEYS as $dayKey) {
            $total = $dayTotals[$dayKey];
            if ($total > 8.0 + 1e-6) {
                $warnings[] = 'day_hours_exceed_8:'.$dayKey;
            }
            if ($total <= 1e-6) {
                $warnings[] = 'day_zero_hours:'.$dayKey;
            }
        }

        return array_values(array_unique($warnings));
    }
}
