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

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

$id = isset($_GET['id']) ? (int)$_GET['id'] : 0;

if (!$id) {
    echo json_encode(['error' => 'Invalid product ID']);
    exit;
}

try {
    $stmt = $pdo->prepare("
        SELECT e.*, c.name as category_name 
        FROM equipment e 
        JOIN categories c ON e.category_id = c.id 
        WHERE e.id = ?
    ");
    $stmt->execute([$id]);
    $product = $stmt->fetch();
    
    if ($product) {
        // DON'T MODIFY THE PATH - Keep it exactly as stored in database
        // The same path that works on index.php and cart.php
        // Just return it as-is
        echo json_encode($product);
    } else {
        echo json_encode(['error' => 'Product not found']);
    }
} catch(PDOException $e) {
    echo json_encode(['error' => $e->getMessage()]);
}
?>