<?php

namespace App\Models;

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

class Restaurant extends Model
{
    use HasFactory;

    protected $fillable = [
        'name',
        'slug',
        'address',
        'phone',
        'email',
        'logo',
        'vat_rate',
        'currency',
        'settings',
        'is_active',
    ];

    protected $casts = [
        'vat_rate' => 'decimal:2',
        'settings' => 'array',
        'is_active' => 'boolean',
    ];

    public function branches(): HasMany
    {
        return $this->hasMany(Branch::class);
    }

    public function users(): HasMany
    {
        return $this->hasMany(User::class);
    }

    public function categories(): HasMany
    {
        return $this->hasMany(Category::class);
    }

    public function menuItems(): HasMany
    {
        return $this->hasMany(MenuItem::class);
    }

    public function orders(): HasMany
    {
        return $this->hasMany(Order::class);
    }

    public function customers(): HasMany
    {
        return $this->hasMany(Customer::class);
    }

    public function inventoryItems(): HasMany
    {
        return $this->hasMany(InventoryItem::class);
    }

    public function getFormattedVatRateAttribute(): string
    {
        return number_format($this->vat_rate, 2) . '%';
    }

    public function calculateVat(float $amount): float
    {
        return $amount * ($this->vat_rate / 100);
    }

    public function calculateTotalWithVat(float $amount): float
    {
        return $amount + $this->calculateVat($amount);
    }

    public function scopeActive($query)
    {
        return $query->where('is_active', true);
    }
}