<?php
require_once 'config.php';

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

if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    http_response_code(405);
    echo json_encode(['success' => false, 'message' => 'Method not allowed']);
    exit;
}

$network_type = $_POST['network_type'] ?? 'mtn';
$role = $_POST['role'] ?? 'agent';

// Validate network type (MTN only)
if ($network_type !== 'mtn') {
    echo json_encode(['success' => false, 'message' => 'Invalid network type. Only MTN is supported.']);
    exit;
}

// Validate role
if ($role !== 'agent') {
    echo json_encode(['success' => false, 'message' => 'Invalid role']);
    exit;
}

// Fetch pricing rules from database
$pricing_query = "SELECT min_size_gb, max_size_gb, price_per_gb FROM agent_pricing_rules WHERE network_type = ? AND role = ? AND is_active = 1 ORDER BY min_size_gb";
$stmt = $conn->prepare($pricing_query);
$stmt->bind_param("ss", $network_type, $role);
$stmt->execute();
$result = $stmt->get_result();

$pricing_rules = [];
while ($row = $result->fetch_assoc()) {
    $pricing_rules[] = [
        'min_size_gb' => floatval($row['min_size_gb']),
        'max_size_gb' => floatval($row['max_size_gb']),
        'price_per_gb' => floatval($row['price_per_gb'])
    ];
}
$stmt->close();

echo json_encode(['success' => true, 'pricing_rules' => $pricing_rules]);
?>
