<?php

namespace App\Models;

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

class VatGstRate extends Model
{
    protected $table = 'vat_gst_rates';
    protected $fillable = ['business_id', 'name', 'rate', 'type', 'is_active'];
    protected $casts = ['is_active' => 'boolean', 'rate' => 'decimal:4'];

    public function toArray(): array
    {
        $a = parent::toArray();
        $a['businessId'] = $a['business_id'] ?? null;
        $a['isActive']   = (bool) ($a['is_active'] ?? true);
        // Frontend reads `percentage` and `status` columns.
        $a['percentage'] = $a['rate'] ?? 0;
        $a['status']     = ($a['is_active'] ?? true) ? 'Active' : 'Inactive';
        return $a;
    }

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