<?php
session_start();
header('Content-Type: application/json');

require_once 'shikanisha.kts.php'; // Assuming this file contains your database connection logic

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    // Initialize variables
    $beverage_name = $_POST['beverage_name'] ?? '';
    $buying_price = $_POST['buying_price'] ?? 0;
    $selling_price = $_POST['selling_price'] ?? 0;
    $quantity = $_POST['bottleQuantity'] ?? 0;
    $qtyper_bottle = $_POST['tot_per_glass'] ?? 0;
    $available_bottle_qty = $_POST['bottleQuantity'] ?? 0;
    $qbprice = ($qtyper_bottle != 0) ? ($buying_price / $qtyper_bottle) : 0; // Avoid division by zero
    $category = $_POST['beverage_category'] ?? '';
    $t_availability = $_POST['t_availability'] ?? 'No'; // Default to 'No' if not provided
    $description = $_POST['totDescription'] ?? '';
    $image_location = '';
    $cat = 'Beverages';
    $initial_date = date('Y-m-d');

    $qbselling_price = 0; // Initialize qbselling_price with a default value
    $id = 0; // Initialize id with a default value

    // Handle image upload
    if (isset($_FILES['beverage_image']) && $_FILES['beverage_image']['error'] == UPLOAD_ERR_OK) {
        $image = $_FILES['beverage_image'];
        $image_name = basename($image['name']);
        $upload_dir = '../uploads/';
        $image_location = $upload_dir . $image_name;

        if (!move_uploaded_file($image['tmp_name'], $image_location)) {
            echo json_encode(['status' => 'error', 'message' => 'Failed to upload image']);
            exit;
        }
    }

    // Prepare and execute insert for the beverages table
    $sql_beverages = "INSERT INTO beverages (beverage_name, buying_price, selling_price, quantity, qtyper_bottle, qbprice, qbselling_price, category, t_availability, image_location, shots_available, cat, initial_date, tot_id, description) 
            VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

    $stmt_beverages = $conn->prepare($sql_beverages);

    if ($stmt_beverages === false) {
        echo json_encode(['status' => 'error', 'message' => 'Error preparing statement: ' . $conn->error]);
        exit;
    }

    // Determine qbselling_price and id based on session 'tots'
    if ($t_availability == 'Yes' && isset($_SESSION['tots'])) {
        foreach ($_SESSION['tots'] as $tots) {
            $id = $tots['id'];
            $qbselling_price = $tots['price']; // Set qbselling_price from session tots
            break; // Assuming we only need the first matching tot
        }
        unset($_SESSION['tots']);
    }

    // Bind parameters
    $stmt_beverages->bind_param("sdddiddsssdssis", $beverage_name, $buying_price, $selling_price, $quantity, $qtyper_bottle, $qbprice, $qbselling_price, $category, $t_availability, $image_location, $available_bottle_qty, $cat, $initial_date, $id, $description);

    // Execute statement
    if ($stmt_beverages->execute()) {
        echo json_encode(['status' => 'success', 'message' => 'Beverage added successfully']);
    } else {
        echo json_encode(['status' => 'error', 'message' => 'Error executing beverages statement: ' . $stmt_beverages->error]);
    }

    $stmt_beverages->close();
} else {
    echo json_encode(['status' => 'error', 'message' => 'Invalid request method']);
}
?>
