<?php
include('shikanisha.kts.php');

$data = [];

// Fetch ingredients
$sql = "SELECT id AS product_id, name, quantity, 'ingredient' AS category FROM ingredients";
$result = $conn->query($sql);

if (!$result) {
    error_log("SQL Error: " . $conn->error);
    header('Content-Type: application/json');
    echo json_encode(['status' => 'error', 'message' => 'Failed to fetch ingredients.']);
    $conn->close();
    exit;
}

while ($row = $result->fetch_assoc()) {
    $data[] = $row;
}

// Fetch beverages
$sql = "SELECT beverage_id AS product_id, beverage_name AS name, quantity AS quantity, 'beverage' AS category FROM beverages";
$result = $conn->query($sql);

if (!$result) {
    error_log("SQL Error: " . $conn->error);
    header('Content-Type: application/json');
    echo json_encode(['status' => 'error', 'message' => 'Failed to fetch beverages.']);
    $conn->close();
    exit;
}

while ($row = $result->fetch_assoc()) {
    $data[] = $row;
}

// Fetch tots
$sql = "SELECT tot_id AS product_id, name, quantity, 'tot' AS category FROM tots";
$result = $conn->query($sql);

if (!$result) {
    error_log("SQL Error: " . $conn->error);
    header('Content-Type: application/json');
    echo json_encode(['status' => 'error', 'message' => 'Failed to fetch tots.']);
    $conn->close();
    exit;
}

while ($row = $result->fetch_assoc()) {
    $data[] = $row;
}

// Send JSON response
header('Content-Type: application/json');
echo json_encode(['data' => $data]);

// Close database connection
$conn->close();
?>
