<template>
  <div class="oauth-callback">
    <div class="container">
      <div class="callback-card">
        <div v-if="loading" class="loading-state">
          <div class="spinner"></div>
          <h2>Authenticating...</h2>
          <p>Please wait while we complete your OAuth login</p>
        </div>

        <div v-else-if="error" class="error-state">
          <div class="error-icon">✕</div>
          <h2>Authentication Failed</h2>
          <p class="error-message">{{ error }}</p>
          <router-link to="/challonge-test" class="btn btn-primary">
            Back to Challonge Test
          </router-link>
        </div>

        <div v-else-if="success" class="success-state">
          <div class="success-icon">✓</div>
          <h2>Authentication Successful!</h2>
          <p>You're now logged in with OAuth</p>
          <p class="redirect-info">Redirecting in {{ countdown }} seconds...</p>
          <router-link to="/challonge-test" class="btn btn-primary">
            Continue to Challonge Test
          </router-link>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import { useChallongeOAuth } from '../composables/useChallongeOAuth.js';

const route = useRoute();
const router = useRouter();
const { exchangeCode } = useChallongeOAuth();

const loading = ref(true);
const error = ref(null);
const success = ref(false);
const countdown = ref(3);

onMounted(async () => {
  // Get authorization code and state from URL
  const code = route.query.code;
  const state = route.query.state;
  const errorParam = route.query.error;
  const errorDescription = route.query.error_description;

  // Handle OAuth errors
  if (errorParam) {
    loading.value = false;
    error.value = errorDescription || `OAuth error: ${errorParam}`;
    return;
  }

  // Validate required parameters
  if (!code || !state) {
    loading.value = false;
    error.value = 'Missing authorization code or state parameter';
    return;
  }

  try {
    // Exchange authorization code for tokens
    await exchangeCode(code, state);

    loading.value = false;
    success.value = true;

    // Start countdown redirect
    const interval = setInterval(() => {
      countdown.value--;
      if (countdown.value <= 0) {
        clearInterval(interval);
        router.push('/challonge-test');
      }
    }, 1000);
  } catch (err) {
    loading.value = false;
    error.value = err.message || 'Failed to complete OAuth authentication';
    console.error('OAuth callback error:', err);
  }
});
</script>

<style scoped>
.oauth-callback {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 2rem;
}

.container {
  max-width: 500px;
  width: 100%;
}

.callback-card {
  background: white;
  border-radius: 12px;
  padding: 3rem;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
  text-align: center;
}

/* Loading State */
.loading-state {
  padding: 2rem 0;
}

.spinner {
  width: 64px;
  height: 64px;
  border: 5px solid #f3f3f3;
  border-top: 5px solid #667eea;
  border-radius: 50%;
  margin: 0 auto 2rem;
  animation: spin 1s linear infinite;
}

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

/* Success State */
.success-state {
  padding: 2rem 0;
}

.success-icon {
  width: 80px;
  height: 80px;
  background: #10b981;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 auto 1.5rem;
  font-size: 3rem;
  color: white;
  animation: scaleIn 0.5s ease;
}

/* Error State */
.error-state {
  padding: 2rem 0;
}

.error-icon {
  width: 80px;
  height: 80px;
  background: #ef4444;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 auto 1.5rem;
  font-size: 3rem;
  color: white;
  animation: scaleIn 0.5s ease;
}

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

h2 {
  font-size: 2rem;
  font-weight: 700;
  margin-bottom: 1rem;
  color: #1f2937;
}

p {
  font-size: 1.1rem;
  color: #6b7280;
  margin-bottom: 1rem;
}

.error-message {
  color: #ef4444;
  font-weight: 500;
  padding: 1rem;
  background: #fee2e2;
  border-radius: 8px;
  margin: 1.5rem 0;
}

.redirect-info {
  font-size: 0.95rem;
  color: #9ca3af;
  margin-top: 1rem;
}

.btn {
  display: inline-block;
  padding: 0.75rem 2rem;
  background: #667eea;
  color: white;
  text-decoration: none;
  border-radius: 8px;
  font-weight: 600;
  margin-top: 1.5rem;
  transition: all 0.3s ease;
  border: none;
  cursor: pointer;
  font-size: 1rem;
}

.btn:hover {
  background: #5568d3;
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(102, 126, 234, 0.4);
}

.btn-primary {
  background: #667eea;
}

.btn-primary:hover {
  background: #5568d3;
}
</style>
