<?php
require_once '../config.php';
require_once '../../includes/Auth.php';
require_once '../../includes/Shop.php';

$auth = new Auth();
$user = $auth->requireAuth();
$shop = new Shop();

if ($_SERVER['REQUEST_METHOD'] === 'GET') {
    $product_id = $_GET['id'] ?? null;

    if (!$product_id) {
        jsonResponse(['success' => false, 'message' => 'Product ID is required'], 400);
    }

    try {
        $product_data = $shop->getProductById($product_id);

        // Any authenticated user should be able to view product details.
        if (!$product_data) {
            jsonResponse(['success' => false, 'message' => 'Product not found'], 404);
        }

        jsonResponse(['success' => true, 'product' => $product_data]);
    } catch (Exception $e) {
        jsonResponse(['success' => false, 'message' => $e->getMessage()], 500);
    }
}