<?php

declare(strict_types=1);

use App\Enums\ApprovalStatus;
use App\Enums\VacationType;
use App\Models\Employee;
use App\Models\Vacation;
use App\Services\Vacation\VacationBalanceService;

beforeEach(function (): void {
    config(['hr.annual_leave_days' => 24]);

    $this->service = new VacationBalanceService;
    $this->employee = Employee::factory()->create();
});

it('returns annual leave entitlement from configuration', function (): void {
    expect($this->service->entitlement())->toBe(24);
});

it('sums approved annual vacation days for the given year', function (): void {
    Vacation::factory()->approved()->create([
        'employee_id' => $this->employee->id,
        'type' => VacationType::Annual,
        'start_date' => '2025-03-01',
        'end_date' => '2025-03-05',
        'days' => 5,
    ]);

    Vacation::factory()->approved()->create([
        'employee_id' => $this->employee->id,
        'type' => VacationType::Annual,
        'start_date' => '2025-06-01',
        'end_date' => '2025-06-03',
        'days' => 3,
    ]);

    Vacation::factory()->pending()->create([
        'employee_id' => $this->employee->id,
        'type' => VacationType::Annual,
        'start_date' => '2025-07-01',
        'end_date' => '2025-07-10',
        'days' => 10,
    ]);

    Vacation::factory()->approved()->create([
        'employee_id' => $this->employee->id,
        'type' => VacationType::Marriage,
        'start_date' => '2025-04-01',
        'end_date' => '2025-04-05',
        'days' => 5,
    ]);

    expect($this->service->takenAnnualDays($this->employee, 2025))->toBe(8);
});

it('calculates remaining annual leave days', function (): void {
    $year = (int) now()->year;

    Vacation::factory()->approved()->create([
        'employee_id' => $this->employee->id,
        'type' => VacationType::Annual,
        'start_date' => "{$year}-02-01",
        'end_date' => "{$year}-02-06",
        'days' => 6,
    ]);

    expect($this->service->remainingAnnualDays($this->employee))->toBe(18);
});

it('ignores rejected annual vacations when calculating taken days', function (): void {
    $year = (int) now()->year;

    Vacation::factory()->pending()->create([
        'employee_id' => $this->employee->id,
        'type' => VacationType::Annual,
        'status' => ApprovalStatus::Rejected,
        'start_date' => "{$year}-01-01",
        'end_date' => "{$year}-01-05",
        'days' => 5,
    ]);

    expect($this->service->takenAnnualDays($this->employee))->toBe(0)
        ->and($this->service->remainingAnnualDays($this->employee))->toBe(24);
});
