<?php
require 'db.php';
header('Content-Type: application/json');

$category_id = (int)($_GET['category_id'] ?? 0);

if ($category_id <= 0) {
    echo json_encode([]);
    exit;
}

$q = mysqli_query($conn,"
    SELECT id, name
    FROM variant_groups
    WHERE category_id = $category_id
      AND status = 'active'
    ORDER BY sort_order ASC, name ASC
");

$data = [];

while ($row = mysqli_fetch_assoc($q)) {
    $data[] = $row;
}

echo json_encode($data);