<?php

namespace App\Observers;

use App\Enums\IsPaid;
use App\Models\Payroll;

class PayrollObserver
{
    public function saving(Payroll $payroll): void
    {
        $payroll->total_salary = $payroll->base_salary + $payroll->bonus - $payroll->deduction;

        if ($payroll->is_paid?->isPaid(IsPaid::PAID) && ! $payroll->paid_at) {
            $payroll->paid_at = now();
        }

        if ($payroll->is_paid?->isPaid(IsPaid::NOT_PAID)) {
            $payroll->paid_at = null;
        }
    }
}
