<?php
require_once 'config.php';

$email = 'elsonfxcobby2020@gmail.com';

// Find the agent
$stmt = $conn->prepare("SELECT id FROM agents WHERE email = ?");
$stmt->bind_param("s", $email);
$stmt->execute();
$result = $stmt->get_result();
$agent = $result->fetch_assoc();
$stmt->close();

if (!$agent) {
    echo "Agent not found\n";
    exit;
}

$agentId = $agent['id'];

echo "=== RECENT ORDERS FOR SUB-AGENT (ID: $agentId) ===\n\n";

// Check recent agent_orders
echo "--- AGENT_ORDERS TABLE ---\n";
$ordersStmt = $conn->prepare("
    SELECT id, package_size, amount, network_type, status, created_at
    FROM agent_orders
    WHERE agent_id = ?
    ORDER BY created_at DESC
    LIMIT 10
");
$ordersStmt->bind_param("i", $agentId);
$ordersStmt->execute();
$ordersResult = $ordersStmt->get_result();

if ($ordersResult->num_rows > 0) {
    while ($row = $ordersResult->fetch_assoc()) {
        echo "Order ID: " . $row['id'] . "\n";
        echo "Package: " . $row['package_size'] . "\n";
        echo "Amount Charged: GH₵ " . number_format($row['amount'], 2) . "\n";
        echo "Network: " . $row['network_type'] . "\n";
        echo "Status: " . $row['status'] . "\n";
        echo "Date: " . $row['created_at'] . "\n";
        echo "---\n";
    }
} else {
    echo "No orders found in agent_orders table\n";
}
$ordersStmt->close();

// Check orders table (store orders)
echo "\n--- ORDERS TABLE (Store Orders) ---\n";
$storeOrdersStmt = $conn->prepare("
    SELECT id, package_size, amount, network_type, status, created_at
    FROM orders
    WHERE agent_id = ?
    ORDER BY created_at DESC
    LIMIT 10
");
$storeOrdersStmt->bind_param("i", $agentId);
$storeOrdersStmt->execute();
$storeOrdersResult = $storeOrdersStmt->get_result();

if ($storeOrdersResult->num_rows > 0) {
    while ($row = $storeOrdersResult->fetch_assoc()) {
        echo "Order ID: " . $row['id'] . "\n";
        echo "Package: " . $row['package_size'] . "\n";
        echo "Amount Charged: GH₵ " . number_format($row['amount'], 2) . "\n";
        echo "Network: " . $row['network_type'] . "\n";
        echo "Status: " . $row['status'] . "\n";
        echo "Date: " . $row['created_at'] . "\n";
        echo "---\n";
    }
} else {
    echo "No orders found in orders table\n";
}
$storeOrdersStmt->close();

// Test what price StorePricingManager would return for 1GB
echo "\n=== TESTING STORE PRICING MANAGER FOR 1GB ===\n";
require_once 'app/classes/StorePricingManager.php';
$storePricingManager = new StorePricingManager($conn);

// Get agent's store_id
$agentStmt = $conn->prepare("SELECT store_id FROM agents WHERE id = ?");
$agentStmt->bind_param("i", $agentId);
$agentStmt->execute();
$agentResult = $agentStmt->get_result();
$agentData = $agentResult->fetch_assoc();
$agentStmt->close();

if ($agentData && $agentData['store_id']) {
    $storeId = $agentData['store_id'];
    echo "Using Store ID: $storeId\n";
    
    $priceInfo = $storePricingManager->calculatePrice($storeId, '1GB', 'mtn');
    echo "Price Result:\n";
    echo "  Price: " . ($priceInfo['price'] !== null ? 'GH₵ ' . number_format($priceInfo['price'], 2) : 'NULL') . "\n";
    echo "  Source: " . $priceInfo['source'] . "\n";
    echo "  Package Size GB: " . $priceInfo['package_size_gb'] . "\n";
    if (isset($priceInfo['price_per_gb'])) {
        echo "  Price per GB: GH₵ " . number_format($priceInfo['price_per_gb'], 2) . "\n";
    }
} else {
    echo "Agent does not have a store_id\n";
}

$conn->close();
?>


