<?php
// Include your database connection file
include('shikanisha.kts.php'); // Update with your actual database connection file

// Get the beverage_id from the request
$beverage_id = isset($_GET['beverage_id']) ? intval($_GET['beverage_id']) : 0;

if ($beverage_id > 0) {
    // Prepare the SQL query to fetch beverage details
    $query = "SELECT beverage_id, beverage_name, buying_price, selling_price, qtyper_bottle, qbprice, qbselling_price, t_availability, description
              FROM beverages
              WHERE beverage_id = ?";

    // Create a prepared statement
    if ($stmt = $conn->prepare($query)) {
        // Bind parameters
        $stmt->bind_param('i', $beverage_id);

        // Execute the query
        $stmt->execute();

        // Bind result variables
        $stmt->bind_result($id, $name, $buying_price, $selling_price, $qtyper_bottle, $qbprice, $qbselling_price, $t_availability, $description);

        // Fetch the result
        if ($stmt->fetch()) {
            // Prepare the response data
            $response = [
                'status' => 'success',
                'data' => [
                    'beverage_id' => $id,
                    'beverage_name' => $name,
                    'buying_price' => $buying_price,
                    'selling_price' => $selling_price,
                    'qtyper_bottle' => $qtyper_bottle,
                    'qbprice' => $qbprice,
                    'qbselling_price' => $qbselling_price,
                    't_availability' => $t_availability,
                    'description' => $description
                ]
            ];
        } else {
            // No data found for the given ID
            $response = [
                'status' => 'error',
                'message' => 'No beverage found with the provided ID.'
            ];
        }

        // Close the statement
        $stmt->close();
    } else {
        // SQL query preparation failed
        $response = [
            'status' => 'error',
            'message' => 'Failed to prepare the SQL query.'
        ];
    }

    // Close the database connection
    $conn->close();
} else {
    // Invalid beverage ID
    $response = [
        'status' => 'error',
        'message' => 'Invalid beverage ID provided.'
    ];
}

// Output the response in JSON format
header('Content-Type: application/json');
echo json_encode($response);
?>
