<template>
  <div class="auth-container">
    <div class="auth-card">
      <div class="auth-header">
        <i class="pi pi-cloud" style="font-size: 2.5rem; color: var(--p-primary-color)"></i>
        <h1>Mini-Cloud</h1>
        <p>Registrieren</p>
      </div>

      <form @submit.prevent="handleRegister" class="auth-form">
        <div class="field">
          <label for="username">Benutzername</label>
          <InputText
            id="username"
            v-model="username"
            placeholder="Benutzername (min. 3 Zeichen)"
            autofocus
            fluid
          />
        </div>

        <div class="field">
          <label for="email">E-Mail (optional)</label>
          <InputText
            id="email"
            v-model="email"
            placeholder="email@beispiel.de"
            type="email"
            fluid
          />
        </div>

        <div class="field">
          <label for="password">Passwort</label>
          <Password
            id="password"
            v-model="password"
            placeholder="Passwort (min. 8 Zeichen)"
            toggle-mask
            fluid
          />
        </div>

        <div class="field">
          <label for="password2">Passwort wiederholen</label>
          <Password
            id="password2"
            v-model="password2"
            placeholder="Passwort wiederholen"
            :feedback="false"
            toggle-mask
            fluid
          />
        </div>

        <Message v-if="error" severity="error" :closable="false">{{ error }}</Message>

        <Button
          type="submit"
          label="Registrieren"
          :loading="loading"
          fluid
        />
      </form>

      <div class="auth-footer">
        <router-link to="/login">Bereits ein Konto? Anmelden</router-link>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { useAuthStore } from '../stores/auth'
import axios from 'axios'
import InputText from 'primevue/inputtext'
import Password from 'primevue/password'
import Button from 'primevue/button'
import Message from 'primevue/message'

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

const username = ref('')
const email = ref('')
const password = ref('')
const password2 = ref('')
const error = ref('')
const loading = ref(false)
const inviteToken = ref('')

onMounted(async () => {
  // Check for invite token in URL
  inviteToken.value = route.query.invite || ''
  if (inviteToken.value) return // Invite links always work

  try {
    const res = await axios.get('/api/auth/registration-status')
    if (!res.data.allowed) {
      router.push('/login')
    }
  } catch { router.push('/login') }
})

async function handleRegister() {
  error.value = ''

  if (password.value !== password2.value) {
    error.value = 'Passwoerter stimmen nicht ueberein'
    return
  }

  loading.value = true
  try {
    await auth.register(username.value, password.value, email.value || undefined, inviteToken.value || undefined)
    router.push('/')
  } catch (err) {
    error.value = err.response?.data?.error || 'Registrierung fehlgeschlagen'
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
.auth-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--p-surface-50);
}

.auth-card {
  background: var(--p-surface-0);
  border-radius: 12px;
  padding: 2.5rem;
  width: 100%;
  max-width: 400px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
}

.auth-header {
  text-align: center;
  margin-bottom: 2rem;
}

.auth-header h1 {
  margin: 0.5rem 0 0.25rem;
  font-size: 1.5rem;
}

.auth-header p {
  color: var(--p-text-muted-color);
  margin: 0;
}

.auth-form .field {
  margin-bottom: 1rem;
}

.auth-form .field label {
  display: block;
  margin-bottom: 0.5rem;
  font-weight: 500;
  font-size: 0.875rem;
}

.auth-form button {
  margin-top: 0.5rem;
}

.auth-footer {
  text-align: center;
  margin-top: 1.5rem;
  font-size: 0.875rem;
}

.auth-footer a {
  color: var(--p-primary-color);
  text-decoration: none;
}
</style>
