<script setup lang="ts">
import { useRouter } from 'vue-router';
import { useI18n } from 'vue-i18n';
import { useAuthStore } from '../stores/auth';

const { t } = useI18n();
const auth = useAuthStore();
const router = useRouter();

function goLogin() {
  const redirect = auth.loginReturnTo || undefined;
  auth.hideLoginPrompt();
  router.push({
    path: '/login',
    query: redirect ? { redirect } : {},
  });
}

function continueBrowsing() {
  auth.loginReturnTo = '';
  auth.hideLoginPrompt();
}
</script>

<template>
  <Teleport to="body">
    <Transition name="fade">
      <div v-if="auth.loginPromptVisible" class="login-overlay" @click.self="continueBrowsing">
        <div class="login-modal">
          <button type="button" class="close-btn" :aria-label="t('auth.continue_browsing')" @click="continueBrowsing">
            ✕
          </button>
          <h2 class="login-title">{{ t('auth.login_required') }}</h2>
          <p class="login-hint">{{ t('auth.login_hint') }}</p>

          <div class="login-actions">
            <button type="button" class="login-submit" @click="goLogin">
              {{ t('auth.go_login') }}
            </button>
            <button type="button" class="login-secondary" @click="continueBrowsing">
              {{ t('auth.continue_browsing') }}
            </button>
          </div>
        </div>
      </div>
    </Transition>
  </Teleport>
</template>

<style scoped>
.login-overlay {
  position: fixed;
  inset: 0;
  z-index: 999;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.5);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
}

.login-modal {
  position: relative;
  width: 90%;
  max-width: 360px;
  background: var(--bg-card-elevated);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 28px 24px 24px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.3), var(--shadow-glow);
}

.close-btn {
  position: absolute;
  top: 12px;
  right: 14px;
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 18px;
  cursor: pointer;
  padding: 4px;
  line-height: 1;
}

.close-btn:hover {
  color: var(--text);
}

.login-title {
  font-size: 18px;
  font-weight: 800;
  color: var(--primary);
  margin: 0 0 6px;
  text-align: center;
}

.login-hint {
  font-size: 12px;
  color: var(--text-muted);
  text-align: center;
  margin: 0 0 24px;
}

.login-actions {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.login-submit {
  padding: 12px;
  border-radius: 8px;
  border: none;
  background: var(--gradient-primary);
  color: #FFFFFF;
  font-size: 15px;
  font-weight: 800;
  cursor: pointer;
  min-height: 44px;
  transition: opacity 0.2s;
}

.login-submit:active {
  opacity: 0.85;
}

.login-secondary {
  padding: 10px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text-muted);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  min-height: 40px;
  transition: color 0.2s, border-color 0.2s;
}

.login-secondary:active {
  color: var(--text);
  border-color: var(--primary);
}

.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.25s ease;
}
.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}
</style>
