<?php
// Test the batch data slicing logic

$host = '162.215.11.244';
$username = 'ac3_marketing_portal';
$password = '=Y^3GIyrG;J)TV]z';
$database = 'ac3_marketing_portal';
$port = 3306;

try {
    $pdo = new PDO("mysql:host=$host;port=$port;dbname=$database;charset=utf8mb4", $username, $password);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    echo "🧪 TESTING BATCH DATA SLICING\n";
    echo str_repeat("=", 50) . "\n";
    
    // Create test data for campaign 27 if it doesn't exist
    $campaign_id = 27;
    $user_id = 2; // Assuming user ID 2 exists
    
    // Check if campaign_user_data exists for this campaign
    $existing_data = $pdo->query("
        SELECT COUNT(*) as count 
        FROM campaign_user_data 
        WHERE campaign_id = {$campaign_id} AND user_id = {$user_id}
    ")->fetch()['count'];
    
    if ($existing_data < 1000) {
        echo "📊 Creating test data for campaign {$campaign_id}...\n";
        
        // Create sample test data (simulate 2000 records)
        for ($i = 1; $i <= 2000; $i++) {
            $pdo->exec("
                INSERT IGNORE INTO campaign_user_data 
                (campaign_id, user_id, master_data_id, status, assigned_at) 
                VALUES ({$campaign_id}, {$user_id}, {$i}, 'sent', NOW())
            ");
        }
        
        echo "✅ Created test data\n\n";
    }
    
    // Get batch information
    $batches = $pdo->query("
        SELECT id, batch_number, batch_size, scheduled_message_id
        FROM scheduled_message_batches 
        WHERE scheduled_message_id = (
            SELECT id FROM scheduled_messages WHERE campaign_id = {$campaign_id} LIMIT 1
        )
        ORDER BY batch_number
    ")->fetchAll();
    
    if (empty($batches)) {
        echo "❌ No batches found for campaign {$campaign_id}\n";
        exit;
    }
    
    // Get total records
    $total_records = $pdo->query("
        SELECT COUNT(*) as count 
        FROM campaign_user_data 
        WHERE campaign_id = {$campaign_id} AND user_id = {$user_id}
    ")->fetch()['count'];
    
    echo "📊 Campaign {$campaign_id} Analysis:\n";
    echo "   Total Records: " . number_format($total_records) . "\n";
    echo "   Number of Batches: " . count($batches) . "\n\n";
    
    // Analyze each batch
    foreach ($batches as $batch) {
        $batch_size = $batch['batch_size'];
        $batch_number = $batch['batch_number'];
        
        // Calculate offset for this batch
        $batch_start_offset = ($batch_number - 1) * $batch_size;
        $batch_end_offset = min($batch_start_offset + $batch_size, $total_records);
        
        echo "🎯 Batch {$batch_number} (ID: {$batch['id']}):\n";
        echo "   Batch Size: " . number_format($batch_size) . "\n";
        echo "   Record Range: " . number_format($batch_start_offset + 1) . " - " . number_format($batch_end_offset) . "\n";
        
        // Get sample records for this batch
        $sample_records = $pdo->query("
            SELECT id, master_data_id, status 
            FROM campaign_user_data 
            WHERE campaign_id = {$campaign_id} AND user_id = {$user_id}
            ORDER BY id ASC 
            LIMIT 5 OFFSET {$batch_start_offset}
        ")->fetchAll();
        
        echo "   Sample Records (first 5):\n";
        foreach ($sample_records as $record) {
            echo "     • ID: {$record['id']}, Master Data: {$record['master_data_id']}, Status: {$record['status']}\n";
        }
        
        // Calculate percentage
        $percentage = round(($batch_size / $total_records) * 100, 1);
        echo "   Percentage: {$percentage}%\n\n";
    }
    
    echo "🎉 BATCH SLICING TEST COMPLETE!\n";
    echo "📱 Now try clicking 'View Details' on different deliveries\n";
    echo "   Each should show different sets of records:\n\n";
    
    foreach ($batches as $batch) {
        $batch_start = (($batch['batch_number'] - 1) * $batch['batch_size']) + 1;
        $batch_end = min($batch_start + $batch['batch_size'] - 1, $total_records);
        echo "   • Delivery {$batch['batch_number']}: Records {$batch_start} - {$batch_end}\n";
    }
    
} catch (Exception $e) {
    echo "❌ Error: " . $e->getMessage() . "\n";
}
?>