<?php
// Test script to verify total assigned vs random distribution

$host = '162.215.11.244';
$username = 'ac3_marketing';
$password = 'WsdF%!fh{nfEtWpz';
$database = 'ac3_marketing_portal';

try {
    $pdo = new PDO("mysql:host=$host;dbname=$database", $username, $password);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    echo "Testing Total Assigned vs Random Distribution\n";
    echo "===========================================\n";
    
    // Test with actual campaign data
    $campaign_id = 31;
    $user_id = 119;
    
    // Get ACTUAL totals (what should be displayed in stats)
    $stmt = $pdo->prepare("
        SELECT 
            COUNT(*) as total_assigned,
            SUM(CASE WHEN status = 'sent' THEN 1 ELSE 0 END) as total_delivered,
            SUM(CASE WHEN status = 'failed' THEN 1 ELSE 0 END) as total_failed
        FROM campaign_user_data 
        WHERE campaign_id = ? AND user_id = ?
    ");
    $stmt->execute([$campaign_id, $user_id]);
    $actual_totals = $stmt->fetch(PDO::FETCH_OBJ);
    
    echo "ACTUAL TOTALS (for stats display):\n";
    echo "• Total Assigned: " . number_format($actual_totals->total_assigned) . "\n";
    echo "• Total Delivered: " . number_format($actual_totals->total_delivered) . "\n"; 
    echo "• Total Failed: " . number_format($actual_totals->total_failed) . "\n\n";
    
    // Get batch info
    $stmt = $pdo->prepare("
        SELECT smb.*, COUNT(*) OVER() as total_batches
        FROM scheduled_message_batches smb
        JOIN scheduled_messages sm ON sm.id = smb.scheduled_message_id
        WHERE sm.campaign_id = ?
        ORDER BY smb.batch_number
        LIMIT 4
    ");
    $stmt->execute([$campaign_id]);
    $batches = $stmt->fetchAll(PDO::FETCH_OBJ);
    
    if (count($batches) > 0) {
        $total_batches = $batches[0]->total_batches;
        
        echo "RANDOM DISTRIBUTION (for delivery details only):\n";
        echo "Batch | Random Delivered | Random Failed\n";
        echo "------|------------------|---------------\n";
        
        $total_random_delivered = 0;
        $total_random_failed = 0;
        
        foreach ($batches as $batch) {
            $batch_number = $batch->batch_number;
            
            // Simulate sent distribution (same logic as controller)
            $seed = ($campaign_id * 100) + ($batch_number + 10);
            srand($seed);
            $sent_percentage = rand(10, 60) / 100;
            $random_delivered = floor($actual_totals->total_delivered * $sent_percentage);
            srand();
            
            // Simulate failed distribution (same logic as controller)  
            $seed = ($campaign_id * 100) + $batch_number;
            srand($seed);
            $failed_percentage = rand(0, 40) / 100;
            $random_failed = floor($actual_totals->total_failed * $failed_percentage);
            srand();
            
            $total_random_delivered += $random_delivered;
            $total_random_failed += $random_failed;
            
            printf("  %d   | %15s | %13s\n", 
                $batch_number, 
                number_format($random_delivered), 
                number_format($random_failed)
            );
        }
        
        echo "------|------------------|---------------\n";
        printf("Total | %15s | %13s\n", 
            number_format($total_random_delivered), 
            number_format($total_random_failed)
        );
        
        echo "\nEXPECTED BEHAVIOR:\n";
        echo "✅ Stats display shows ACTUAL totals (not random)\n";
        echo "✅ View Details shows RANDOM distribution of data\n";
        echo "✅ Each delivery batch shows different random amounts\n";
        echo "✅ Export matches what's shown in each batch's view details\n";
    }
    
} catch (PDOException $e) {
    echo "❌ Error: " . $e->getMessage() . "\n";
}
?>