<?php
include '../db.php';

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

$modifier_id = isset($_GET['modifier_id']) ? intval($_GET['modifier_id']) : 0;

if ($modifier_id <= 0) {
    echo json_encode(['success' => false, 'message' => 'Invalid modifier ID']);
    exit;
}

try {
    $query = "
        SELECT 
            img.id,
            img.display_name,
            img.name
        FROM item_modifier_groups img
        JOIN modifier_group_assignments mga ON img.id = mga.modifier_group_id
        WHERE mga.modifier_id = ? AND img.is_active = 1
        ORDER BY img.sort_order, img.display_name
    ";
    
    $stmt = $conn->prepare($query);
    $stmt->bind_param("i", $modifier_id);
    $stmt->execute();
    $result = $stmt->get_result();
    
    $groups = [];
    while ($group = $result->fetch_assoc()) {
        $groups[] = $group;
    }
    
    echo json_encode(['success' => true, 'groups' => $groups]);
    
} catch (Exception $e) {
    echo json_encode(['success' => false, 'message' => 'Database error: ' . $e->getMessage()]);
}
?>
