<script setup lang="ts">
import { ref, onMounted, onBeforeUnmount, watch } from 'vue';
import LoginForm from './LoginForm.vue';
import RegisterForm from './RegisterForm.vue';
import { X } from 'lucide-vue-next';

const props = defineProps<{
    showLogin: boolean;
    showRegister: boolean;
}>();

const emit = defineEmits<{
    (e: 'close'): void;
    (e: 'switch', type: 'login' | 'register'): void;
}>();

const handleClose = () => emit('close');
const switchTo = (type: 'login' | 'register') => emit('switch', type);

function onKey(e: KeyboardEvent) {
    if (e.key === 'Escape') handleClose();
}

onMounted(() => document.addEventListener('keydown', onKey));
onBeforeUnmount(() => document.removeEventListener('keydown', onKey));

// Lock body scroll when modal is open
watch(() => props.showLogin || props.showRegister, (open) => {
    document.body.style.overflow = open ? 'hidden' : '';
}, { immediate: true });
</script>

<template>
    <transition name="auth-fade">
        <div v-if="showLogin || showRegister" class="auth-overlay" @click.self="handleClose">
            <div class="auth-card" :class="{ 'wide': showRegister }">

                <!-- Decorative glow bg -->
                <div class="auth-glow-1"></div>
                <div class="auth-glow-2"></div>

                <!-- Close -->
                <button class="auth-close" @click="handleClose" aria-label="Close">
                    <X class="w-4 h-4" />
                </button>

                <!-- Brand header -->
                <div class="auth-brand">
                    <div class="brand-logo">Beti<span>X</span></div>
                    <div class="brand-tagline">The Ultimate Crypto Casino</div>
                </div>

                <!-- Social proof bar -->
                <div class="auth-proof">
                    <span class="proof-dot"></span>
                    <span>50,000+ active players</span>
                    <span class="proof-sep">·</span>
                    <span>Instant withdrawals</span>
                    <span class="proof-sep">·</span>
                    <span>Provably fair</span>
                </div>

                <!-- Login view -->
                <div v-if="showLogin" class="auth-body">
                    <div class="auth-title-block">
                        <h2 class="auth-title">Welcome <span>Back</span></h2>
                        <p class="auth-sub">Log in to access your account</p>
                    </div>
                    <LoginForm :onSuccess="handleClose">
                        <template #forgot-password>
                            <a href="/forgot-password" class="forgot-link">Forgot password?</a>
                        </template>
                    </LoginForm>
                    <div class="auth-switch">
                        Don't have an account?
                        <button @click="switchTo('register')" class="switch-btn">Create one free</button>
                    </div>
                </div>

                <!-- Register view -->
                <div v-if="showRegister" class="auth-body">
                    <div class="auth-title-block">
                        <h2 class="auth-title">Create <span>Account</span></h2>
                        <p class="auth-sub">Join the ultimate crypto protocol</p>
                    </div>
                    <RegisterForm :onSuccess="handleClose" />
                    <div class="auth-switch">
                        Already have an account?
                        <button @click="switchTo('login')" class="switch-btn">Log in</button>
                    </div>
                </div>

            </div>
        </div>
    </transition>
</template>

<style scoped>
.auth-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.88);
    backdrop-filter: blur(12px);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 9000;
    padding: 16px;
}

.auth-card {
    position: relative;
    background: #0a0a0a;
    border: 1px solid rgba(255, 255, 255, 0.06);
    border-radius: 24px;
    width: 100%;
    max-width: 460px;
    overflow: hidden;
    box-shadow:
        0 0 0 1px rgba(223, 0, 106, 0.08),
        0 40px 80px rgba(0, 0, 0, 0.7),
        0 0 60px rgba(223, 0, 106, 0.06);
}

.auth-card.wide {
    max-width: 520px;
}

/* Decorative glows */
.auth-glow-1 {
    position: absolute;
    top: -60px;
    right: -60px;
    width: 200px;
    height: 200px;
    background: radial-gradient(circle, rgba(223, 0, 106, 0.15) 0%, transparent 70%);
    pointer-events: none;
    z-index: 0;
}

.auth-glow-2 {
    position: absolute;
    bottom: -60px;
    left: -40px;
    width: 160px;
    height: 160px;
    background: radial-gradient(circle, rgba(0, 242, 255, 0.07) 0%, transparent 70%);
    pointer-events: none;
    z-index: 0;
}

.auth-close {
    position: absolute;
    top: 16px;
    right: 16px;
    width: 32px;
    height: 32px;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.05);
    border: 1px solid rgba(255, 255, 255, 0.08);
    color: #666;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 0.2s;
    z-index: 10;
}

.auth-close:hover {
    color: #fff;
    background: rgba(223, 0, 106, 0.2);
    border-color: rgba(223, 0, 106, 0.4);
    transform: rotate(90deg);
}

/* Brand header */
.auth-brand {
    position: relative;
    z-index: 1;
    padding: 28px 32px 0;
    text-align: center;
}

.brand-logo {
    font-size: 2rem;
    font-weight: 900;
    color: #fff;
    letter-spacing: -0.02em;
}

.brand-logo span {
    color: var(--primary, #df006a);
}

.brand-tagline {
    font-size: 11px;
    color: #444;
    text-transform: uppercase;
    letter-spacing: 0.12em;
    font-weight: 700;
    margin-top: 2px;
}

/* Social proof */
.auth-proof {
    position: relative;
    z-index: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    font-size: 11px;
    color: #555;
    font-weight: 600;
    padding: 12px 32px 0;
}

.proof-dot {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: #00ff9d;
    box-shadow: 0 0 6px #00ff9d;
    animation: proof-pulse 2s infinite;
    flex-shrink: 0;
}

@keyframes proof-pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.4; }
}

.proof-sep { color: #333; }

/* Body */
.auth-body {
    position: relative;
    z-index: 1;
    padding: 24px 32px 32px;
    max-height: calc(90vh - 120px);
    overflow-y: auto;
}

.auth-body::-webkit-scrollbar { width: 4px; }
.auth-body::-webkit-scrollbar-track { background: transparent; }
.auth-body::-webkit-scrollbar-thumb { background: #222; border-radius: 2px; }

.auth-title-block {
    text-align: center;
    margin-bottom: 24px;
}

.auth-title {
    font-size: 1.75rem;
    font-weight: 900;
    color: #fff;
    letter-spacing: -0.02em;
    margin: 0 0 4px;
    line-height: 1.1;
}

.auth-title span {
    color: var(--primary, #df006a);
}

.auth-sub {
    font-size: 13px;
    color: #666;
    margin: 0;
}

/* Switch prompt */
.auth-switch {
    text-align: center;
    margin-top: 20px;
    font-size: 12px;
    color: #555;
}

.switch-btn {
    color: var(--primary, #df006a);
    font-weight: 700;
    background: none;
    border: none;
    cursor: pointer;
    padding: 0 0 0 4px;
    transition: opacity 0.15s;
}

.switch-btn:hover { opacity: 0.75; text-decoration: underline; }

/* Forgot link */
.forgot-link {
    font-size: 12px;
    color: #555;
    text-decoration: none;
    transition: color 0.15s;
}

.forgot-link:hover { color: var(--primary, #df006a); }

/* Transition */
.auth-fade-enter-active, .auth-fade-leave-active {
    transition: opacity 0.2s ease;
}
.auth-fade-enter-active .auth-card,
.auth-fade-leave-active .auth-card {
    transition: transform 0.25s cubic-bezier(0.2, 0, 0, 1), opacity 0.2s ease;
}
.auth-fade-enter-from { opacity: 0; }
.auth-fade-leave-to { opacity: 0; }
.auth-fade-enter-from .auth-card { transform: scale(0.95) translateY(12px); }
.auth-fade-leave-to .auth-card { transform: scale(0.97); }

@media (max-width: 480px) {
    .auth-card { border-radius: 16px; }
    .auth-body { padding: 20px 20px 24px; }
    .auth-brand { padding: 24px 20px 0; }
    .auth-proof { padding: 10px 20px 0; flex-wrap: wrap; gap: 4px; }
    .auth-title { font-size: 1.5rem; }
}
</style>
