<?php
require_once 'auth.php';
requireAdminLogin();

require_once '../config/database.php';
$pdo = getDBConnection();

echo "<h2>🚀 Fixing All Tatkal Issues</h2>";
echo "<style>
    body { font-family: Arial, sans-serif; margin: 20px; }
    .success { color: green; font-weight: bold; }
    .error { color: red; font-weight: bold; }
    .info { color: blue; }
</style>";

try {
    echo "<h3>1. 💰 Fixing Price (₹1500)</h3>";
    
    // Update tatkal_price in settings table
    $stmt = $pdo->prepare("INSERT INTO settings (setting_key, setting_value) VALUES (?, ?) ON DUPLICATE KEY UPDATE setting_value = ?");
    $stmt->execute(['tatkal_price', '1500', '1500']);
    echo "<span class='success'>✅ Settings table: tatkal_price = 1500</span><br>";
    
    // Update tatkal_booking consultation type price
    $stmt = $pdo->prepare("UPDATE consultation_types SET base_price = 1500.00, is_active = 1 WHERE type_code = 'tatkal_booking'");
    $stmt->execute();
    echo "<span class='success'>✅ Consultation types: tatkal_booking price = 1500</span><br>";
    
    echo "<h3>2. 📅 Fixing Days (Saturday & Sunday Only)</h3>";
    
    // Disable all days first
    $allDays = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday'];
    
    foreach ($allDays as $day) {
        $settingKey = 'tatkal_' . $day . '_enabled';
        $stmt = $pdo->prepare("INSERT INTO settings (setting_key, setting_value) VALUES (?, ?) ON DUPLICATE KEY UPDATE setting_value = ?");
        $stmt->execute([$settingKey, '0', '0']);
    }
    echo "<span class='info'>🔄 All days disabled first</span><br>";
    
    // Enable only Saturday and Sunday
    $enabledDays = ['saturday', 'sunday'];
    
    foreach ($enabledDays as $day) {
        $settingKey = 'tatkal_' . $day . '_enabled';
        $stmt = $pdo->prepare("INSERT INTO settings (setting_key, setting_value) VALUES (?, ?) ON DUPLICATE KEY UPDATE setting_value = ?");
        $stmt->execute([$settingKey, '1', '1']);
        echo "<span class='success'>✅ " . ucfirst($day) . " enabled</span><br>";
    }
    
    echo "<h3>3. ⚡ Ensuring Tatkal is Enabled</h3>";
    
    // Enable tatkal
    $stmt = $pdo->prepare("INSERT INTO settings (setting_key, setting_value) VALUES (?, ?) ON DUPLICATE KEY UPDATE setting_value = ?");
    $stmt->execute(['tatkal_enabled', '1', '1']);
    echo "<span class='success'>✅ Tatkal enabled</span><br>";
    
    echo "<h3>4. 🔍 Final Verification</h3>";
    
    // Verify tatkal_price
    $stmt = $pdo->prepare("SELECT setting_value FROM settings WHERE setting_key = 'tatkal_price'");
    $stmt->execute();
    $settingsPrice = $stmt->fetchColumn();
    echo "<span class='info'>Settings tatkal_price: ₹$settingsPrice</span><br>";
    
    // Verify consultation type price
    $stmt = $pdo->prepare("SELECT base_price, is_active FROM consultation_types WHERE type_code = 'tatkal_booking'");
    $stmt->execute();
    $consultation = $stmt->fetch(PDO::FETCH_ASSOC);
    if ($consultation) {
        $status = $consultation['is_active'] ? 'ACTIVE' : 'INACTIVE';
        echo "<span class='info'>Consultation type price: ₹{$consultation['base_price']} ($status)</span><br>";
    }
    
    // Verify enabled days
    echo "<span class='info'>Enabled days: ";
    $enabledCount = 0;
    foreach ($allDays as $day) {
        $settingKey = 'tatkal_' . $day . '_enabled';
        $stmt = $pdo->prepare("SELECT setting_value FROM settings WHERE setting_key = ?");
        $stmt->execute([$settingKey]);
        $value = $stmt->fetchColumn() ?: '0';
        if ($value === '1') {
            echo ucfirst($day) . " ";
            $enabledCount++;
        }
    }
    echo "($enabledCount days)</span><br>";
    
    if ($settingsPrice == '1500' && $consultation['base_price'] == '1500.00' && $enabledCount == 2) {
        echo "<h3><span class='success'>🎉 ALL ISSUES FIXED SUCCESSFULLY!</span></h3>";
        echo "<span class='success'>✅ Price: ₹1500</span><br>";
        echo "<span class='success'>✅ Days: Saturday & Sunday only</span><br>";
        echo "<span class='success'>✅ Tatkal: Enabled</span><br>";
    } else {
        echo "<h3><span class='error'>⚠️ Some issues may remain</span></h3>";
    }
    
} catch (Exception $e) {
    echo "<span class='error'>❌ Error: " . $e->getMessage() . "</span>";
}

echo "<br><br><a href='tatkal_management_panel.php'>← Back to Tatkal Management</a>";
echo " | <a href='debug_tatkal.php'>🔍 Debug Tatkal</a>";
echo " | <a href='../api/test_db.php' target='_blank'>🧪 Test API</a>";
?>
