<?php

namespace App\Modules\HR\Loans\Models;

use App\Modules\Admin\Tenancy\Traits\BelongsToTenant;
use App\Modules\HR\Employees\Models\Employee;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;

class Loan extends Model
{
    use BelongsToTenant;

    protected $table = 'hr_loans';

    protected $fillable = [
        'tenant_id', 'employee_id', 'type', 'amount', 'interest_rate',
        'installment_count', 'installment_amount', 'start_date', 'status',
        'remaining_balance', 'reason', 'notes', 'approved_by', 'approved_at', 'created_by',
    ];

    protected function casts(): array
    {
        return [
            'amount' => 'decimal:2', 'interest_rate' => 'decimal:4',
            'installment_amount' => 'decimal:2', 'remaining_balance' => 'decimal:2',
            'start_date' => 'date', 'approved_at' => 'datetime',
        ];
    }

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

    public function installments(): HasMany
    {
        return $this->hasMany(LoanInstallment::class);
    }
}
