<?php
/**
 * Debug Status Display Issue
 * Check what records are being returned and their status values
 */

// Database connection
$mysqli = new mysqli('162.215.11.244', 'ac3_marketing_portal', '=Y^3GIyrG;J)TV]z', 'ac3_marketing_portal');

if ($mysqli->connect_error) {
    die('Connection failed: ' . $mysqli->connect_error);
}

echo "=== DEBUG STATUS DISPLAY ISSUE ===\n";
echo "Checking what status values are in the database\n\n";

// Check status distribution for LICBIMALAKSHMI campaign
$campaign_id = 31;
$user_id = 1; // Assuming user ID 1, adjust as needed

echo "Campaign ID: $campaign_id\n";
echo "User ID: $user_id\n\n";

// Check total status distribution
$status_query = "
    SELECT 
        status,
        COUNT(*) as count,
        ROUND((COUNT(*) * 100.0 / (SELECT COUNT(*) FROM campaign_user_data WHERE campaign_id = $campaign_id AND user_id = $user_id)), 2) as percentage
    FROM campaign_user_data 
    WHERE campaign_id = $campaign_id AND user_id = $user_id
    GROUP BY status
    ORDER BY count DESC
";

echo "Status Distribution:\n";
echo "===================\n";
$result = $mysqli->query($status_query);
while ($row = $result->fetch_assoc()) {
    printf("%-12s: %10s records (%6.2f%%)\n", 
           strtoupper($row['status']), 
           number_format($row['count']), 
           $row['percentage']);
}

echo "\nSample Records Check:\n";
echo "====================\n";

// Get sample sent records
$sent_sample = $mysqli->query("
    SELECT id, status, 'sent_sample' as record_type
    FROM campaign_user_data 
    WHERE campaign_id = $campaign_id AND user_id = $user_id AND status = 'sent'
    LIMIT 5
");

echo "Sample SENT records:\n";
while ($row = $sent_sample->fetch_assoc()) {
    echo "ID: {$row['id']}, Status: '{$row['status']}'\n";
}

// Get sample failed records  
$failed_sample = $mysqli->query("
    SELECT id, status, 'failed_sample' as record_type
    FROM campaign_user_data 
    WHERE campaign_id = $campaign_id AND user_id = $user_id AND status = 'failed'
    LIMIT 5
");

echo "\nSample FAILED records:\n";
while ($row = $failed_sample->fetch_assoc()) {
    echo "ID: {$row['id']}, Status: '{$row['status']}'\n";
}

echo "\nBatch Distribution Simulation:\n";
echo "=============================\n";

// Simulate what Batch 1 should show
$batch_number = 1;
$total_batches = 4;

// Get actual totals
$sent_total_result = $mysqli->query("
    SELECT COUNT(*) as count FROM campaign_user_data 
    WHERE campaign_id = $campaign_id AND user_id = $user_id AND status = 'sent'
");
$total_sent = $sent_total_result->fetch_assoc()['count'];

$failed_total_result = $mysqli->query("
    SELECT COUNT(*) as count FROM campaign_user_data 
    WHERE campaign_id = $campaign_id AND user_id = $user_id AND status = 'failed'
");
$total_failed = $failed_total_result->fetch_assoc()['count'];

// Apply batch distribution logic
$seed = ($campaign_id * 100) + $batch_number;
srand($seed);
$failed_percentage = rand(0, 40);
$batch_failed_count = floor(($total_failed * $failed_percentage) / 100);
srand();

$seed = intval($campaign_id . ($batch_number + 10));
srand($seed);
$sent_percentage = rand(10, 60);
$batch_sent_count = floor(($total_sent * $sent_percentage) / 100);
srand();

// Calculate page distribution
$page_size = 25;
$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 "Batch 1 should show on first page:\n";
echo "- $sent_on_page records with status = 'sent'\n";
echo "- $failed_on_page records with status = 'failed'\n";
echo "- Total: " . ($sent_on_page + $failed_on_page) . " records per page\n\n";

echo "✅ EXPECTED BEHAVIOR:\n";
echo "In the data table, you should see:\n";
echo "- Green 'Sent' badges for delivered records\n";
echo "- Red 'Failed' badges for failed records\n";
echo "- Mixed distribution on each page\n\n";

echo "❌ IF YOU DON'T SEE 'SENT' STATUS:\n";
echo "1. Check if records actually have 'sent' status in database\n";
echo "2. Verify the DataTable is loading data via AJAX correctly\n";
echo "3. Check browser developer console for JavaScript errors\n";
echo "4. Inspect the JSON response from the AJAX endpoint\n";

$mysqli->close();
?>