<?php
header('Content-Type: application/json');
require_once 'config.php';

$customer_id = isset($_GET['customer_id']) ? intval($_GET['customer_id']) : 0;
$favorites = [];

if ($customer_id > 0) {
    $sql = "SELECT
                f.menu_item_id,
                m.id AS id,
                m.name AS name,
                m.price AS price,
                m.image AS image,
                'menu_item' AS item_type
            FROM
                favorites f
            INNER JOIN
                menu_items m ON f.menu_item_id = m.id
            WHERE
                f.fav_customer_id = 2

            UNION

            SELECT
                f.menu_item_id,
                a.id AS id,
                a.name AS name,
                a.price AS price,
                a.image AS image,
                'deal_item' AS item_type
            FROM
                favorites f
            INNER JOIN
                app_menu_deals_items a ON f.menu_item_id = a.id
            WHERE
                f.fav_customer_id = ?;";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("i", $customer_id);
    $stmt->execute();
    $result = $stmt->get_result();
    while ($row = $result->fetch_assoc()) {
        $favorites[] = $row;
    }
    $stmt->close();
}

echo json_encode(['success' => true, 'favorites' => $favorites]);
