<?php

namespace App\Models;

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

class MenuCategory extends Model
{
    use HasFactory;

    protected $fillable = [
        'name',
        'description',
        'image',
        'icon',
        'is_active',
        'order'
    ];

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

    protected $appends = ['image_url'];

    // Relationship: Satu kategori memiliki banyak item
    public function items()
    {
        return $this->hasMany(MenuItem::class, 'category_id');
    }

    // Relationship: Hanya item yang aktif
    public function activeItems()
    {
        return $this->hasMany(MenuItem::class, 'category_id')
                    ->where('is_active', true)
                    ->orderBy('order');
    }

    // Accessor: URL gambar lengkap
    public function getImageUrlAttribute()
	{
    return $this->image ? asset($this->image) : null;
	}
    // Scopes
    public function scopeActive($query)
    {
        return $query->where('is_active', true);
    }

    public function scopeOrdered($query)
    {
        return $query->orderBy('order');
    }
}