<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class PayrollItem extends Model
{
    use HasFactory;

    protected $fillable = [
        'user_id',
        'payroll_id',
        'employee_id',
        'employee_type',

        'rate_monthly_salary',
        'rate_per_roz',
        'rate_overtime_rate',

        'total_present_days',
        'total_absent_days',
        'total_leave_days',
        'total_holiday_days',

        'total_overtime_hours',
        'total_roz',

        'gross_salary',
        'overtime_amount',
        'deduction_amount',
        'advance_adjusted',
        'net_salary',

        'paid_amount',
        'due_amount',

        'generated_at',
        'is_locked',
        'remarks',
    ];

    protected $casts = [
        // IDs (important fix)
        'user_id'     => 'integer',
        'payroll_id'  => 'integer',
        'employee_id' => 'integer',

        // Rates
        'rate_monthly_salary'   => 'decimal:2',
        'rate_per_roz'          => 'decimal:2',
        'rate_overtime_rate'    => 'decimal:2',

        // Days
        'total_present_days'    => 'decimal:2',
        'total_absent_days'     => 'decimal:2',
        'total_leave_days'      => 'decimal:2',
        'total_holiday_days'    => 'decimal:2',

        // Hours & Roz
        'total_overtime_hours'  => 'decimal:2',
        'total_roz'             => 'decimal:2',

        // Salary
        'gross_salary'          => 'decimal:2',
        'overtime_amount'       => 'decimal:2',
        'deduction_amount'      => 'decimal:2',
        'advance_adjusted'      => 'decimal:2',
        'net_salary'            => 'decimal:2',

        // Payment
        'paid_amount'           => 'decimal:2',
        'due_amount'            => 'decimal:2',

        // Others
        'generated_at'          => 'datetime',
        'is_locked'             => 'boolean',
    ];

    public function user()
    {
        return $this->belongsTo(User::class);
    }

    public function payroll()
    {
        return $this->belongsTo(Payroll::class);
    }

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

    public function payments()
    {
        return $this->hasMany(PayrollPayment::class);
    }
}