<?php

declare(strict_types=1);

namespace App\Observers;

use App\Enums\ApprovalStatus;
use App\Models\Vacation;
use App\Services\Leave\LeaveDaysCalculator;

class VacationObserver
{
    public function __construct(
        private readonly LeaveDaysCalculator $leaveDaysCalculator,
    ) {}

    public function creating(Vacation $vacation): void
    {
        if ($vacation->status === null) {
            $vacation->status = ApprovalStatus::Pending;
        }
    }

    public function saving(Vacation $vacation): void
    {
        if ($vacation->start_date === null || $vacation->end_date === null) {
            return;
        }

        $vacation->days = $this->leaveDaysCalculator->between(
            $vacation->start_date,
            $vacation->end_date,
        );
    }
}
