<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class LoanDocument extends Model
{
    use SoftDeletes;

    protected $table = 'loan_document_master';
    protected $primaryKey = 'LD_Id';

    protected $fillable = [
        'LD_Code',
        'LD_Name',
        'LT_Id',
        'Is_Mandatory',
        'Is_Original',
        'Loan_Doc',
        'Is_Active'
    ];

    protected $casts = [
        'Loan_Doc' => 'array',
        'Is_Mandatory' => 'boolean',
        'Is_Original' => 'boolean',
        'Is_Active' => 'boolean'
    ];

    // Relation
    public function loanType()
    {
        return $this->belongsTo(LoanType::class, 'LT_Id', 'LT_Id');
    }

    // ✅ FIXED ACCESSOR
    protected $appends = ['loan_doc_urls'];

    public function getLoanDocUrlsAttribute()
    {
        if (!$this->Loan_Doc || !is_array($this->Loan_Doc)) {
            return [];
        }

        return collect($this->Loan_Doc)
            ->pluck('url')
            ->filter()
            ->values();
    }
}
