<?php

namespace App\Modules\HR\Shifts\Support;

use App\Modules\HR\Shifts\Models\Shift;
use Carbon\CarbonImmutable;

/**
 * Payroll-ready overtime minutes after configured threshold (no pay rules).
 */
final class ShiftOvertimePreview
{
    public static function minutesPastOvertimeThreshold(
        Shift $shift,
        string $attendanceDateYmd,
        CarbonImmutable $checkout,
    ): int {
        $day = CarbonImmutable::parse($attendanceDateYmd)->startOfDay();
        $scheduledEnd = ShiftScheduleEndpoints::shiftEndOnDate($shift, $day);
        $threshold = $scheduledEnd->addMinutes(max(0, (int) $shift->overtime_after_minutes));
        if ($checkout->lte($threshold)) {
            return 0;
        }

        return (int) $threshold->diffInMinutes($checkout);
    }
}
