<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class ChartOfAccount extends Model
{
    protected $table = 'chart_of_accounts';

    protected $fillable = ['business_id', 'code', 'name', 'type', 'tax_type', 'description', 'is_active'];

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

    public function toArray(): array
    {
        $a = parent::toArray();
        $a['businessId'] = $a['business_id'] ?? null;
        $a['taxType']    = $a['tax_type']    ?? null;
        $a['isActive']   = (bool) ($a['is_active'] ?? true);
        // Frontend reads `category` for the account name column.
        $a['category']   = $a['name'] ?? null;
        $a['status']     = ($a['is_active'] ?? true) ? 'Active' : 'Inactive';
        return $a;
    }

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