<template>
  <div class="oauth-callback">
    <div class="loading-container">
      <h2>{{ status }}</h2>
      <div class="spinner"></div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { useAuthStore } from '../stores/authStore'

const route = useRoute()
const router = useRouter()
const authStore = useAuthStore()

const status = ref('Authentifizierung läuft...')

onMounted(async () => {
  const token = route.query.token
  const error = route.query.error

  if (error) {
    status.value = 'OAuth-Login fehlgeschlagen'
    setTimeout(() => {
      router.push('/login')
    }, 2000)
    return
  }

  if (token) {
    try {
      // Token speichern
      authStore.saveToken(token)
      
      // Benutzer-Daten laden
      await authStore.fetchCurrentUser()
      
      status.value = 'Login erfolgreich! Sie werden weitergeleitet...'
      
      setTimeout(() => {
        router.push('/')
      }, 1000)
    } catch (err) {
      status.value = 'Fehler beim Login'
      setTimeout(() => {
        router.push('/login')
      }, 2000)
    }
  } else {
    status.value = 'Kein Token erhalten'
    setTimeout(() => {
      router.push('/login')
    }, 2000)
  }
})
</script>

<style scoped>
.oauth-callback {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #ffffff;
}

.loading-container {
  text-align: center;
}

.loading-container h2 {
  color: #333;
  font-weight: 500;
  margin-bottom: 2rem;
}

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

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



