<?php
require_once 'config.php';

$email = 'elsonfxcobby2020@gmail.com';

// Find the agent
$stmt = $conn->prepare("SELECT id, email, store_id, username 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 with email: $email\n";
    exit;
}

echo "=== AGENT INFORMATION ===\n";
echo "Agent ID: " . $agent['id'] . "\n";
echo "Email: " . $agent['email'] . "\n";
echo "Username: " . $agent['username'] . "\n";
echo "Store ID: " . ($agent['store_id'] ?? 'NULL') . "\n\n";

if (empty($agent['store_id'])) {
    echo "ERROR: Agent does not have a store_id assigned!\n";
    exit;
}

$storeId = $agent['store_id'];

// Get store owner information
$storeStmt = $conn->prepare("SELECT id, owner_id, store_name FROM stores WHERE id = ?");
$storeStmt->bind_param("i", $storeId);
$storeStmt->execute();
$storeResult = $storeStmt->get_result();
$store = $storeResult->fetch_assoc();
$storeStmt->close();

if ($store) {
    echo "=== STORE INFORMATION ===\n";
    echo "Store ID: " . $store['id'] . "\n";
    echo "Store Name: " . $store['store_name'] . "\n";
    echo "Store Owner ID: " . $store['owner_id'] . "\n\n";
}

// Check store pricing for 1GB (MTN network)
echo "=== STORE PRICING FOR 1GB (MTN) ===\n";
$pricingStmt = $conn->prepare("
    SELECT id, network_type, min_size_gb, max_size_gb, price_per_gb, 
           (price_per_gb * 1.0) as price_for_1gb, is_active
    FROM store_pricing
    WHERE store_id = ? AND network_type = 'mtn'
    AND min_size_gb <= 1.0 AND max_size_gb >= 1.0
    AND is_active = 1
    ORDER BY min_size_gb DESC
");
$pricingStmt->bind_param("i", $storeId);
$pricingStmt->execute();
$pricingResult = $pricingStmt->get_result();

if ($pricingResult->num_rows > 0) {
    while ($row = $pricingResult->fetch_assoc()) {
        echo "Pricing Rule ID: " . $row['id'] . "\n";
        echo "Network: " . $row['network_type'] . "\n";
        echo "Size Range: " . $row['min_size_gb'] . " GB - " . $row['max_size_gb'] . " GB\n";
        echo "Price per GB: GH₵ " . number_format($row['price_per_gb'], 2) . "\n";
        echo "Price for 1GB: GH₵ " . number_format($row['price_for_1gb'], 2) . "\n";
        echo "Active: " . ($row['is_active'] ? 'Yes' : 'No') . "\n";
        echo "---\n";
    }
} else {
    echo "NO PRICING FOUND for 1GB on MTN network!\n";
    echo "Checking all pricing rules for this store...\n\n";
    
    // Show all pricing rules for this store
    $allPricingStmt = $conn->prepare("
        SELECT id, network_type, min_size_gb, max_size_gb, price_per_gb, is_active
        FROM store_pricing
        WHERE store_id = ? AND is_active = 1
        ORDER BY network_type, min_size_gb
    ");
    $allPricingStmt->bind_param("i", $storeId);
    $allPricingStmt->execute();
    $allPricingResult = $allPricingStmt->get_result();
    
    if ($allPricingResult->num_rows > 0) {
        echo "Available pricing rules:\n";
        while ($row = $allPricingResult->fetch_assoc()) {
            echo "  - " . $row['network_type'] . ": " . $row['min_size_gb'] . "-" . $row['max_size_gb'] . " GB @ GH₵ " . number_format($row['price_per_gb'], 2) . "/GB\n";
        }
    } else {
        echo "NO PRICING RULES FOUND for this store!\n";
    }
    $allPricingStmt->close();
}
$pricingStmt->close();

// Also check admin pricing as fallback
echo "\n=== ADMIN PRICING (FALLBACK) FOR 1GB (MTN) ===\n";
$adminPricingStmt = $conn->prepare("
    SELECT price_per_gb, (price_per_gb * 1.0) as price_for_1gb
    FROM agent_pricing_rules
    WHERE network_type = 'mtn' AND role = 'agent' AND is_active = 1
    AND min_size_gb <= 1.0 AND max_size_gb >= 1.0
    ORDER BY min_size_gb DESC
    LIMIT 1
");
$adminPricingStmt->execute();
$adminPricingResult = $adminPricingStmt->get_result();

if ($adminPricingResult->num_rows > 0) {
    $adminRow = $adminPricingResult->fetch_assoc();
    echo "Admin Price per GB: GH₵ " . number_format($adminRow['price_per_gb'], 2) . "\n";
    echo "Admin Price for 1GB: GH₵ " . number_format($adminRow['price_for_1gb'], 2) . "\n";
} else {
    echo "No admin pricing found for 1GB\n";
}
$adminPricingStmt->close();

$conn->close();
?>


