<?php
// app/Models/Tax.php

namespace App\Models;

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

class Tax extends Model
{
    use HasFactory;

    protected $fillable = [
        'name', 'slug', 'rate', 'type', 'description', 'is_active', 'is_default'
    ];

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

    public function products()
    {
        return $this->hasMany(Product::class);
    }

    public function sales()
    {
        return $this->hasMany(Sale::class);
    }

    public static function getDefaultTax()
    {
        return self::where('is_default', true)->where('is_active', true)->first();
    }

    public static function getActiveTaxes()
    {
        return self::where('is_active', true)->get();
    }

    public function calculateTax($amount)
    {
        if ($this->type == 'percentage') {
            return ($amount * $this->rate) / 100;
        }
        return $this->rate; // Fixed amount
    }
}