<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Flora - Key System Completed</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
        }

        body {
            background-color: #000000;
            min-height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
            color: #fff;
            position: relative;
            overflow: hidden;
        }

        .background-effect {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: radial-gradient(circle at 50% 50%, #1a1a1a, #000000);
            z-index: 0;
        }

        .energy-orb {
            position: absolute;
            width: 300px;
            height: 300px;
            background: radial-gradient(circle at center, #ffffff, #808080, #000000);
            border-radius: 50%;
            filter: blur(50px);
            opacity: 0.5;
            transition: all 0.3s ease;
        }

        .container {
            background: rgba(20, 20, 20, 0.8);
            backdrop-filter: blur(10px);
            border: 1px solid rgba(255, 255, 255, 0.1);
            border-radius: 20px;
            padding: 2rem;
            width: 90%;
            max-width: 400px;
            text-align: center;
            position: relative;
            z-index: 1;
        }

        .logo {
            width: 80px;
            height: 80px;
            margin-bottom: 1.5rem;
            transition: transform 0.3s ease;
        }

        .logo:hover {
            transform: scale(1.1);
        }

        h1 {
            font-size: 2.5rem;
            font-weight: bold;
            margin-bottom: 1rem;
            background: linear-gradient(45deg, #ffffff, #808080);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            animation: titleGlow 2s ease-in-out infinite alternate;
        }

        @keyframes titleGlow {
            from {
                text-shadow: 0 0 5px rgba(255, 255, 255, 0.5);
            }
            to {
                text-shadow: 0 0 20px rgba(255, 255, 255, 0.8);
            }
        }

        button {
            align-items: center;
            background-image: linear-gradient(144deg, #af40ff, #5b42f3 50%, #00ddeb);
            border: 0;
            border-radius: 8px;
            box-shadow: rgba(151, 65, 252, 0.2) 0 15px 30px -5px;
            box-sizing: border-box;
            color: #ffffff;
            display: flex;
            font-size: 18px;
            justify-content: center;
            line-height: 1em;
            max-width: 100%;
            min-width: 140px;
            padding: 3px;
            text-decoration: none;
            user-select: none;
            -webkit-user-select: none;
            touch-action: manipulation;
            white-space: nowrap;
            cursor: pointer;
            transition: all 0.3s;
            margin: 20px auto;
        }

        button:active,
        button:hover {
            outline: 0;
        }

        button span {
            background-color: rgb(5, 6, 45);
            padding: 16px 24px;
            border-radius: 6px;
            width: 100%;
            height: 100%;
            transition: 300ms;
        }

        button:hover span {
            background: none;
        }

        button:active {
            transform: scale(0.9);
        }

        button:disabled {
            opacity: 0.6;
            cursor: not-allowed;
        }

        .message {
            margin-top: 15px;
            font-size: 16px;
            color: #cccccc;
        }

        #keyDisplay, #verificationResult {
            margin-top: 20px;
            font-weight: bold;
            color: #4CAF50;
        }

        .tos {
            margin-top: 20px;
            font-size: 14px;
            color: #888;
        }

        .verification-animation {
            width: 60px;
            height: 60px;
            margin: 20px auto;
            border: 3px solid rgba(255, 255, 255, 0.3);
            border-top: 3px solid #af40ff;
            border-radius: 50%;
            animation: spin 1s linear infinite, grow 0.5s ease-out;
        }

        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }

        @keyframes grow {
            0% { transform: scale(0); opacity: 0; }
            50% { transform: scale(1.2); opacity: 1; }
            100% { transform: scale(1); opacity: 1; }
        }

        .verification-complete {
            border-color: rgba(255, 255, 255, 0.3);
            border-top-color: #00ddeb;
        }

        .verification-failed {
            border-color: rgba(255, 255, 255, 0.3);
            border-top-color: #ff4444;
        }

        .checkmark {
            display: none;
            color: #00ddeb;
            font-size: 24px;
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
        }

        .verification-complete .checkmark {
            display: block;
        }
    </style>
    <!-- Add Turnstile script -->
    <script src="https://challenges.cloudflare.com/turnstile/v0/api.js" async defer></script>
</head>
<body>
    <div class="background-effect"></div>
    <div class="energy-orb"></div>
    <div class="container">
        <img src="https://hebbkx1anhila5yf.public.blob.vercel-storage.com/9f59ec83f4c9f39bd85bed08d55d7b8f-q7otptCnVDmHU3AC9pVZKSAzNnkpFo.png" alt="Flora Logo" class="logo">
        <h1>Key System Completed!</h1>
        <div class="message">Verifying your Linkvertise hash...</div>
        <div class="verification-animation" id="verificationAnimation">
            <span class="checkmark"></span>
        </div>
        <div id="verificationResult"></div>
        <!-- Add Turnstile widget -->
        <div class="cf-turnstile" data-sitekey="0x4AAAAAAA6V1WLCNZZkM3kz" data-callback="onTurnstileSuccess"></div>
        <button id="getKeyButton" style="display: none;">
            <span class="text">Get Your Key!</span>
        </button>
        <div id="keyDisplay"></div>
        <div class="tos">By using Flora, you agree to our ToS.</div>
    </div>

    <script>
        const serverEndpoint = 'https://server-production-08bd.up.railway.app/validate';
        const getKeyButton = document.getElementById('getKeyButton');
        const verificationResult = document.getElementById('verificationResult');
        const keyDisplay = document.getElementById('keyDisplay');
        const verificationAnimation = document.getElementById('verificationAnimation');
        let turnstileToken = null;

        function getParameterByName(name, url = window.location.href) {
            name = name.replace(/[\[\]]/g, '\\$&');
            var regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)'),
                results = regex.exec(url);
            if (!results) return null;
            if (!results[2]) return '';
            return decodeURIComponent(results[2].replace(/\+/g, ' '));
        }

        async function verifyHash() {
            const hash = getParameterByName('hash');

            if (!hash) {
                verificationResult.textContent = 'Error: No hash found in URL.';
                verificationAnimation.classList.add('verification-failed');
                return;
            }

            try {
                const response = await fetch(serverEndpoint, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({
                        hash,
                        ip: 'user-ip-here', // Replace with actual client IP if available
                        turnstileToken: turnstileToken // Add Turnstile token
                    }),
                });

                if (!response.ok) {
                    const errorData = await response.json();
                    throw new Error(errorData.message || 'Verification failed.');
                }

                const result = await response.json();

                if (result.status === 'success') {
                    verificationResult.textContent = 'Hash verified successfully!';
                    verificationResult.style.color = '#00ddeb';
                    verificationAnimation.classList.add('verification-complete');
                    verificationAnimation.style.animation = 'none';
                    getKeyButton.style.display = 'flex';
                } else {
                    throw new Error('Hash verification failed.');
                }
            } catch (error) {
                console.error('Error:', error);
                verificationResult.textContent = 'Hash verification failed. Please try again.';
                verificationResult.style.color = '#ff4444';
                verificationAnimation.classList.add('verification-failed');
            }
        }

        async function generateKey() {
            getKeyButton.disabled = true;
            getKeyButton.innerHTML = "<span class='text'>Generating....</span>";
            
            try {
                const response = await fetch("https://gateway.getflora.xyz/generate-key", { 
                    method: "POST",
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({
                        turnstileToken: turnstileToken // Add Turnstile token
                    })
                });
                const data = await response.json();
                
                if (data.success) {
                    keyDisplay.innerText = "Here is your 24h key! " + data.key;
                    getKeyButton.innerHTML = "<span class='text'>Generated</span>";
                } else {
                    throw new Error("Could not generate key.");
                }
            } catch (error) {
                keyDisplay.innerText = "Error: Could not generate key. Please try again.";
                getKeyButton.innerHTML = "<span class='text'>Get Your Key!</span>";
                getKeyButton.disabled = false;
            }
        }

        getKeyButton.addEventListener('click', generateKey);

        // Energy orb animation
        const orb = document.querySelector('.energy-orb');
        let mouseX = 0, mouseY = 0;
        let currentX = 0, currentY = 0;

        document.addEventListener('mousemove', (e) => {
            mouseX = e.clientX;
            mouseY = e.clientY;
        });

        function animate() {
            const dx = mouseX - currentX;
            const dy = mouseY - currentY;

            currentX += dx * 0.1;
            currentY += dy * 0.1;

            orb.style.left = `${currentX - 150}px`;
            orb.style.top = `${currentY - 150}px`;

            requestAnimationFrame(animate);
        }

        animate();

        // Turnstile callback function
        function onTurnstileSuccess(token) {
            turnstileToken = token;
            verifyHash(); // Verify hash after Turnstile is completed
        }

        // Do not automatically verify hash on page load
        // window.onload = verifyHash;
    </script>
</body>
</html>

