<?php

namespace App\Models;

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

class TaxRate extends Model
{
    use HasFactory;

    protected $fillable = [
        'province_code',
        'province_name',
        'gst_rate',
        'pst_rate',
        'hst_rate',
        'total_rate',
        'effective_date',
        'stripe_tax_code',
    ];

    protected $casts = [
        'gst_rate' => 'decimal:3',
        'pst_rate' => 'decimal:3',
        'hst_rate' => 'decimal:3',
        'total_rate' => 'decimal:3',
        'effective_date' => 'date',
    ];

    /**
     * Boot method to automatically calculate total rate
     */
    protected static function boot()
    {
        parent::boot();

        static::saving(function ($taxRate) {
            $taxRate->total_rate = $taxRate->gst_rate + $taxRate->pst_rate + $taxRate->hst_rate;
        });
    }
}
