<?php
/**
 * Test Improved Batch Distribution
 * Verify the new proportional mixing logic
 */

echo "=== TESTING IMPROVED BATCH DISTRIBUTION ===\n";
echo "Testing new proportional mixing logic\n\n";

function testImprovedDistribution($campaign_id, $batch_number, $total_sent, $total_failed, $page_size = 25) {
    echo "Campaign ID: $campaign_id, Batch: $batch_number\n";
    echo "Total Sent: " . number_format($total_sent) . ", Total Failed: " . number_format($total_failed) . "\n";
    
    // Apply the same logic as the improved controller
    // Failed distribution
    $seed = ($campaign_id * 100) + $batch_number;
    srand($seed);
    $failed_percentage = rand(0, 40);
    $batch_failed_count = floor(($total_failed * $failed_percentage) / 100);
    srand();
    
    // Sent distribution
    $seed = intval($campaign_id . ($batch_number + 10));
    srand($seed);
    $sent_percentage = rand(10, 60);
    $batch_sent_count = floor(($total_sent * $sent_percentage) / 100);
    srand();
    
    echo "Batch Gets - Failed: " . number_format($batch_failed_count) . " ({$failed_percentage}%)";
    echo ", Sent: " . number_format($batch_sent_count) . " ({$sent_percentage}%)\n";
    
    // NEW IMPROVED LOGIC: Proportional mixing
    if ($batch_failed_count > 0 && $batch_sent_count > 0) {
        $total_batch_records = $batch_failed_count + $batch_sent_count;
        $failed_ratio = $batch_failed_count / $total_batch_records;
        
        $failed_on_page = min($page_size, max(1, floor($page_size * $failed_ratio)));
        $sent_on_page = $page_size - $failed_on_page;
        
        echo "✅ MIXED PAGE: {$sent_on_page} sent + {$failed_on_page} failed = {$page_size} total\n";
        echo "   Failed ratio: " . round($failed_ratio * 100, 1) . "%\n";
    } else if ($batch_failed_count > 0) {
        $failed_on_page = min($page_size, $batch_failed_count);
        $sent_on_page = $page_size - $failed_on_page;
        echo "❌ FAILED ONLY: {$sent_on_page} sent + {$failed_on_page} failed = " . ($sent_on_page + $failed_on_page) . " total\n";
    } else {
        $sent_on_page = min($page_size, $batch_sent_count);
        $failed_on_page = 0;
        echo "✅ SENT ONLY: {$sent_on_page} sent + {$failed_on_page} failed = " . ($sent_on_page + $failed_on_page) . " total\n";
    }
    
    echo str_repeat("-", 70) . "\n";
}

echo "Testing LICBIMALAKSHMI campaign (ID: 31) with new logic:\n\n";

// Test all 4 batches
for ($batch = 1; $batch <= 4; $batch++) {
    testImprovedDistribution(31, $batch, 8059500, 40500);
    echo "\n";
}

echo "=== COMPARISON: OLD vs NEW LOGIC ===\n\n";

echo "OLD LOGIC (Failed records first):\n";
echo "- Page fills with failed records first\n";
echo "- Sent records only appear after all failed are shown\n";
echo "- Result: Only failed records visible on first pages\n\n";

echo "NEW LOGIC (Proportional mixing):\n";
echo "- Calculate ratio of failed to total batch records\n";
echo "- Apply same ratio to each page\n";
echo "- Result: Every page shows proper mix of sent + failed\n\n";

echo "✅ IMPROVEMENT COMPLETE!\n";
echo "Now each page will show a proper mix of sent and failed records\n";
echo "based on the batch's actual distribution ratio.\n";

?>