<?php

namespace App\Models;

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

class ChartOfAccount extends Model
{
    use HasFactory;

    // Menunjuk ke tabel m_coa yang Anda gunakan (bernama 'accounts' di database)
    protected $table = 'accounts'; 
    
    // Membuka mass-assignment, jadi branch_id aman saat di-insert/update
    protected $guarded = ['id'];

    protected $casts = [
        'is_active' => 'boolean',
    ];

    // ---------------------------------------------------------
    // RELASI ENTERPRISE ERP
    // ---------------------------------------------------------

    // 1. Relasi ke Cabang (Untuk Branch Mapping & Isolasi Data)
    public function branch()
    {
        return $this->belongsTo(Branch::class, 'branch_id');
    }

    // 2. Relasi ke Jurnal Detail (General Ledger)
    // Standar ERP: Semua transaksi bermuara ke debit/kredit GL, bukan tabel expense tunggal
    public function glDetails()
    {
        return $this->hasMany(GlDetail::class, 'account_id');
    }

    // 3. Relasi Legacy (Opsional - Jika masih butuh transisi dari tabel expenses lama)
    public function expenses()
    {
        return $this->hasMany(Expense::class, 'fund_account_id');
    }
}