<?php

namespace App\Models;

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

class Menu extends Model
{
    use HasFactory;

    protected $fillable = [
        'name',
        'available_date',
        'meal_time',
        'is_published',
    ];

    protected $casts = [
        'available_date' => 'date',
        'is_published' => 'boolean',
    ];

    public function meals()
    {
        return $this->belongsToMany(Meal::class, 'menu_meals')
                     ->withPivot('sort_order')
                     ->withTimestamps()
                     ->orderByPivot('sort_order');
    }

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

    public function scopeForDate($query, $date)
    {
        return $query->where('available_date', $date);
    }

    public function scopeUpcoming($query)
    {
        return $query->where('available_date', '>=', now()->toDateString());
    }
}
