<?php

declare(strict_types=1);

namespace App\Services\Vacation;

use App\Enums\ApprovalStatus;
use App\Enums\VacationType;
use App\Models\Employee;

class VacationBalanceService
{
    public function entitlement(): int
    {
        return (int) config('hr.annual_leave_days');
    }

    public function takenAnnualDays(Employee $employee, ?int $year = null): int
    {
        $year ??= (int) now()->year;

        return (int) $employee->vacations()
            ->where('type', VacationType::Annual)
            ->where('status', ApprovalStatus::Approved)
            ->whereYear('start_date', $year)
            ->sum('days');
    }

    public function remainingAnnualDays(Employee $employee): int
    {
        return $this->entitlement() - $this->takenAnnualDays($employee);
    }
}
