<?php

namespace App;

use App;
use Illuminate\Database\Eloquent\Model;

class EmployeeOvertime extends Model
{
    protected $fillable = [
        'employee_id','year','month',
        'ot_hours','ot_amount','ot_multiplier','notes',
    ];

    public function employee()
    {
        return $this->belongsTo(Employees::class);
    }

    /**
     * Auto-compute ot_amount before saving.
     *
     * Formula:
     *   hourly_rate = basic_salary ÷ (working_days × daily_hours)
     *   ot_amount   = hourly_rate × ot_hours × ot_multiplier
     *
     * Defaults: 26 working days/month, 8 hours/day → 208 hours/month
     */
    protected static function booted(): void
    {
        static::saving(function (EmployeeOvertime $ot) {
            $employee       = Employees::find($ot->employee_id);
            $monthlyHours   = 26 * 8;   // 208 — adjust if needed
            $hourlyRate     = $employee ? ($employee->basic_salary / $monthlyHours) : 0;
            $ot->ot_amount  = round($hourlyRate * $ot->ot_hours * $ot->ot_multiplier, 2);
        });
    }
}