<?php

namespace App\Http\Controllers;

use App\Models\MenuItem;
use App\Models\MenuCategory;
use App\Models\Cart;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Auth;

class MenuController extends Controller
{
    public function index()
    {
        $categories = MenuCategory::all();
        $menus = MenuItem::with('category')->where('is_available', true)->get();
        
        // Ambil cart dari database
        $cartItems = Cart::where('user_id', Auth::id())
            ->with('menu')
            ->get();
        
        $totals = Cart::calculateTotals($cartItems);
        
        // Convert cart items untuk tampilan
        $cart = [];
        foreach ($cartItems as $item) {
            $cart[$item->menu_id] = [
                "id" => $item->menu_id,
                "name" => $item->menu->name,
                "quantity" => $item->quantity,
                "price" => $item->price,
                "image" => $item->menu->image,
                "subtotal" => $item->subtotal
            ];
        }

        return view('menu', compact('menus', 'categories', 'cart', 'totals'));
    }
}