<?php
require_once 'includes/config.php';
require_once 'includes/functions.php';

header('Content-Type: application/json');

try {
    // Get a random product
    $product = getRandomProduct($pdo);
    
    if ($product) {
        // Return the product data
        echo json_encode([
            'success' => true,
            'product' => [
                'id' => $product['id'],
                'name' => $product['name'],
                'price' => $product['price'],
                'image' => $product['image'] ? (SITE_URL . '/' . $product['image']) : '',
                'category' => $product['category']
            ]
        ]);
    } else {
        echo json_encode([
            'success' => false,
            'message' => 'No products found'
        ]);
    }
} catch (Exception $e) {
    error_log("Error fetching random product: " . $e->getMessage());
    echo json_encode([
        'success' => false,
        'message' => 'Error fetching product'
    ]);
}

exit();