<?php

declare(strict_types=1);

namespace App\Observers;

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

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

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

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

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