<?php
require_once 'config.php';
require_once 'app/classes/CustomPricingManager.php';

header('Content-Type: application/json; charset=utf-8');

// Initialize pricing manager
$pricingManager = new CustomPricingManager($conn);

// Get parameters
$packageSize = $_POST['package_size'] ?? '';
$networkType = $_POST['network_type'] ?? 'all';
$userRole = $_POST['user_role'] ?? 'all';

if (empty($packageSize)) {
    echo json_encode([
        'success' => false,
        'message' => 'Package size is required'
    ]);
    exit;
}

try {
    // Calculate price using the pricing manager's calculatePrice method
    $pricingResult = $pricingManager->calculatePrice($packageSize, $networkType, $userRole);
    
    if ($pricingResult && $pricingResult['price'] !== null) {
        echo json_encode([
            'success' => true,
            'pricing' => [
                'cost' => $pricingResult['price'],
                'source' => $pricingResult['source'],
                'pricing_id' => $pricingResult['pricing_id']
            ]
        ]);
    } else {
        echo json_encode([
            'success' => false,
            'message' => 'No pricing found for this package size'
        ]);
    }
} catch (Exception $e) {
    echo json_encode([
        'success' => false,
        'message' => 'Error retrieving pricing: ' . $e->getMessage()
    ]);
}
?>

