<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;

#[Fillable([
    'branch_id', 'category_id', 'name', 'slug', 'description', 'price',
    'dine_in_price', 'delivery_price', 'takeaway_price',
    'gst_percentage', 'tax_inclusive',
    'available_dine_in', 'available_delivery', 'available_takeaway',
    'image_path', 'is_veg', 'status', 'sort_order', 'is_active',
])]
class Item extends Model
{
    use SoftDeletes;

    protected $casts = [
        'price' => 'decimal:2',
        'dine_in_price' => 'decimal:2',
        'delivery_price' => 'decimal:2',
        'takeaway_price' => 'decimal:2',
        'gst_percentage' => 'decimal:2',
        'tax_inclusive' => 'boolean',
        'available_dine_in' => 'boolean',
        'available_delivery' => 'boolean',
        'available_takeaway' => 'boolean',
        'is_veg' => 'boolean',
        'is_active' => 'boolean',
    ];

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

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

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

    /**
     * Scope a query to only include items of a specific branch.
     */
    public function scopeForBranch($query, $branchId)
    {
        return $query->where('items.branch_id', $branchId);
    }
}
