<?php

namespace App\Models;

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

class MenuItem extends Model
{
    use HasFactory;

    protected $fillable = [
        'restaurant_id',
        'name',
        'description',
        'price',
        'category',
        'image',
        'is_available',
        'is_popular',
        'preparation_time_minutes',
        'ingredients',
        'allergens',
        'calories',
    ];

    protected $casts = [
        'is_available' => 'boolean',
        'is_popular' => 'boolean',
        'price' => 'decimal:2',
        'calories' => 'decimal:2',
    ];

    public function restaurant()
    {
        return $this->belongsTo(Restaurant::class);
    }

    public function foodOrderItems()
    {
        return $this->hasMany(FoodOrderItem::class);
    }

    public function getFormattedPrice()
    {
        return number_format($this->price, 0) . ' د.ع';
    }

    public function getPreparationTimeFormatted()
    {
        return $this->preparation_time_minutes . ' دقيقة';
    }

    public function getCaloriesFormatted()
    {
        return $this->calories ? $this->calories . ' سعرة حرارية' : 'غير محدد';
    }

    public function getImageUrl()
    {
        if ($this->image) {
            return asset('storage/' . $this->image);
        }
        return asset('images/default-food.jpg');
    }
}