<?php
// Test script to verify completely random failed records distribution

// Database connection
$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 Completely Random Failed Records Distribution\n";
    echo "===================================================\n";
    
    // Get a campaign with failed records
    $stmt = $pdo->prepare("
        SELECT campaign_id, user_id, 
               SUM(CASE WHEN status = 'failed' THEN 1 ELSE 0 END) as failed_count,
               COUNT(*) as total_records
        FROM campaign_user_data 
        GROUP BY campaign_id, user_id 
        HAVING failed_count > 10 AND total_records > 1000
        LIMIT 1
    ");
    $stmt->execute();
    $campaign = $stmt->fetch(PDO::FETCH_OBJ);
    
    if (!$campaign) {
        echo "No suitable campaign found with failed records.\n";
        exit;
    }
    
    echo "Using Campaign ID: {$campaign->campaign_id}, User ID: {$campaign->user_id}\n";
    echo "Total records: {$campaign->total_records}\n";
    echo "Failed records: {$campaign->failed_count}\n";
    
    // Get all failed record IDs
    $stmt = $pdo->prepare("
        SELECT id 
        FROM campaign_user_data 
        WHERE campaign_id = ? AND user_id = ? AND status = 'failed'
        ORDER BY RAND()
    ");
    $stmt->execute([$campaign->campaign_id, $campaign->user_id]);
    $failed_records = $stmt->fetchAll(PDO::FETCH_OBJ);
    
    // Get total batches
    $stmt = $pdo->prepare("
        SELECT COUNT(*) as batch_count
        FROM scheduled_message_batches smb
        JOIN scheduled_messages sm ON sm.id = smb.scheduled_message_id
        WHERE sm.campaign_id = ?
    ");
    $stmt->execute([$campaign->campaign_id]);
    $total_batches = $stmt->fetch(PDO::FETCH_OBJ)->batch_count;
    
    echo "Total batches: {$total_batches}\n\n";
    
    // Simulate completely random distribution
    echo "Random Distribution Simulation (5 runs):\n";
    echo "Run | Batch 1 | Batch 2 | Batch 3 | Batch 4 | Total\n";
    echo "----|---------|---------|---------|---------|------\n";
    
    for ($run = 1; $run <= 5; $run++) {
        $batch_counts = array_fill(1, $total_batches, 0);
        
        // Randomly assign each failed record to a batch
        foreach ($failed_records as $record) {
            $random_batch = rand(1, $total_batches);
            $batch_counts[$random_batch]++;
        }
        
        printf(" %d  ", $run);
        $total_check = 0;
        for ($i = 1; $i <= $total_batches; $i++) {
            printf("| %-7d ", $batch_counts[$i]);
            $total_check += $batch_counts[$i];
        }
        printf("| %d\n", $total_check);
    }
    
    echo "\nNote: Each run shows different random distribution!\n";
    echo "Some batches may have 0 failed, others may have many.\n";
    echo "This is completely random - not equal 25% distribution.\n";
    
} catch (PDOException $e) {
    echo "Error: " . $e->getMessage() . "\n";
}
?>