<?php
// ─── Products API ────────────────────────────────────────────────────────────
// General catalog reads, used by the checkout flow's plan picker (orders.php).
// (services.php@featured/@packages cover the curated homepage/plan-page subsets.)

function productList() {
    global $conn;
    $htype = $_GET['type'] ?? '';
    $where = "status='0'";
    if ($htype !== '') $where .= " AND htype='" . mysqli_real_escape_string($conn, $htype) . "'";

    $rss = mysqli_query($conn, "SELECT * FROM h_product WHERE $where ORDER BY htype, price ASC");
    $items = [];
    while ($row = mysqli_fetch_assoc($rss)) {
        $items[] = [
            'id' => $row['id'], 'name' => $row['pkname'], 'htype' => $row['htype'],
            'space' => $row['space'], 'band' => $row['band'],
            'price' => $row['price'], 'fprice' => $row['fprice'], 'yprice' => $row['yprice'],
            'fmonth' => $row['fmonth'], 'cpanel' => $row['cpanel'], 'whm' => $row['whm']
        ];
    }
    success_response(['items' => $items]);
}

function detail() {
    global $conn;
    $pid = (int)($_GET['pid'] ?? 0);
    $row = mysqli_fetch_assoc(mysqli_query($conn, "SELECT * FROM h_product WHERE id='$pid' AND status='0'"));
    if (!$row) error_response('Product not found', 404);

    success_response([
        'id' => $row['id'], 'name' => $row['pkname'], 'htype' => $row['htype'],
        'space' => $row['space'], 'band' => $row['band'],
        'price' => $row['price'], 'fprice' => $row['fprice'], 'yprice' => $row['yprice'],
        'fmonth' => $row['fmonth'], 'cpanel' => $row['cpanel'], 'whm' => $row['whm']
    ]);
}
