<?php

namespace App\Models;

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

class Cart extends Model
{
    use HasFactory;

    protected $fillable = [
        'user_id',
        'menu_id',
        'quantity',
        'price',
        'subtotal',
        'notes'
    ];

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

    public function menu(): BelongsTo
    {
        return $this->belongsTo(MenuItem::class, 'menu_id');
    }

    public static function calculateTotals($items)
    {
        $subtotal = 0;
        foreach ($items as $item) {
            $subtotal += ($item->price * $item->quantity);
        }

        return [
            'subtotal' => $subtotal,
            'total' => $subtotal
        ];
    }
}
