<?php
include 'includes/config.php';

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

$product_id = $_GET['product_id'] ?? '';

if(!$product_id){
    echo json_encode(['error'=>'Product ID missing']);
    exit;
}

try {
  
    $stmt = $conn->prepare("SELECT sub_sku, default_sell_price, default_purchase_price 
                            FROM variations 
                            WHERE product_id = :product_id LIMIT 1");
    $stmt->bindValue(':product_id', $product_id);
    $stmt->execute();
    $data = $stmt->fetch(PDO::FETCH_ASSOC);

    if(!$data){
       
        $stmt2 = $conn->prepare("SELECT sku FROM products WHERE id = :product_id LIMIT 1");
        $stmt2->bindValue(':product_id', $product_id);
        $stmt2->execute();
        $product = $stmt2->fetch(PDO::FETCH_ASSOC);
        $data = [
            'sub_sku' => $product['sku'] ?? '',
            'default_sell_price' => 0,
            'default_purchase_price' => 0
        ];
    }

    echo json_encode($data);

} catch(PDOException $e) {
    echo json_encode(['error' => $e->getMessage()]);
}
?>
