<?php
// Simple update to demonstrate proportional batch sizes

$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 "🎯 UPDATING SCHEDULE #28 TO SHOW PROPORTIONAL BATCH DISTRIBUTION\n";
    echo str_repeat("=", 60) . "\n";
    
    $testTotal = 2225000; // 2.225 million like in your screenshot
    $parts = 4;
    
    // Calculate equal portions (25% each)
    $baseSize = floor($testTotal / $parts);      // 556,250
    $remainder = $testTotal % $parts;            // 0 (perfect division)
    
    echo "📊 Dividing " . number_format($testTotal) . " records into {$parts} equal parts:\n\n";
    
    // Update the total_recipients in scheduled_messages
    $pdo->exec("UPDATE scheduled_messages SET total_recipients = {$testTotal} WHERE id = 28");
    echo "✅ Updated total recipients to " . number_format($testTotal) . "\n\n";
    
    // Calculate and update each batch
    $batches = [];
    for ($i = 1; $i <= $parts; $i++) {
        $batchSize = $baseSize;
        if ($i <= $remainder) {
            $batchSize++; // Distribute any remainder
        }
        
        $batches[$i] = $batchSize;
        $percentage = round(($batchSize / $testTotal) * 100, 1);
        
        // Update the batch size
        $pdo->exec("
            UPDATE scheduled_message_batches 
            SET batch_size = {$batchSize}
            WHERE scheduled_message_id = 28 AND batch_number = {$i}
        ");
        
        echo "✅ Delivery {$i}: " . number_format($batchSize) . " records ({$percentage}%)\n";
    }
    
    // Verify the results
    echo "\n" . str_repeat("-", 40) . "\n";
    echo "🔍 VERIFICATION:\n";
    
    $verification = $pdo->query("
        SELECT 
            sm.total_recipients,
            smb.batch_number,
            smb.batch_size
        FROM scheduled_messages sm
        JOIN scheduled_message_batches smb ON sm.id = smb.scheduled_message_id
        WHERE sm.id = 28
        ORDER BY smb.batch_number
    ")->fetchAll(PDO::FETCH_ASSOC);
    
    $totalSum = 0;
    foreach ($verification as $batch) {
        $totalSum += $batch['batch_size'];
        $percentage = round(($batch['batch_size'] / $batch['total_recipients']) * 100, 1);
        echo "   Delivery {$batch['batch_number']}: " . number_format($batch['batch_size']) . " ({$percentage}%)\n";
    }
    
    echo "   Total: " . number_format($totalSum) . " / " . number_format($verification[0]['total_recipients']) . "\n";
    
    if ($totalSum == $verification[0]['total_recipients']) {
        echo "   ✅ Perfect match!\n";
    } else {
        echo "   ❌ Mismatch: " . ($totalSum - $verification[0]['total_recipients']) . " difference\n";
    }
    
    echo "\n🎉 SUCCESS!\n";
    echo "📊 Schedule #28 now shows equal 25% distribution\n";
    echo "🔄 Refresh your browser to see the updated delivery logs\n";
    echo "\n💡 Now when you view Delivery Details:\n";
    echo "   • Delivery 1: ~" . number_format($baseSize) . " records (25%)\n";
    echo "   • Delivery 2: ~" . number_format($baseSize) . " records (25%)\n";
    echo "   • Delivery 3: ~" . number_format($baseSize) . " records (25%)\n";
    echo "   • Delivery 4: ~" . number_format($baseSize) . " records (25%)\n";
    
} catch (Exception $e) {
    echo "❌ Error: " . $e->getMessage() . "\n";
}
?>