<template>
  <div class="setup-container">
    <h2>Setup Admin Account</h2>
    <form @submit.prevent="setup">
      <div>
        <input
          v-model="form.login"
          placeholder="Admin login (min 3 chars)"
          required
          minlength="3"
        />
      </div>
      <div>
        <input
          v-model="form.password"
          type="password"
          placeholder="Password (min 6 chars)"
          required
          minlength="6"
        />
      </div>
      <button type="submit">Create Admin</button>
    </form>
    <p v-if="error" class="error">{{ error }}</p>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'

const router = useRouter()
const form = ref({ login: '', password: '' })
const error = ref('')

async function setup() {
  try {
    const res = await fetch('/api/setup', {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify(form.value)
    })

    const data = await res.json()

    if (res.ok) {
      router.push('/login')
    } else {
      error.value = data.error || 'Setup failed'
    }
  } catch (e) {
    error.value = 'Network error'
  }
}
</script>

<style scoped>
.setup-container {
  max-width: 300px;
  margin: 100px auto;
}
input {
  width: 100%;
  padding: 8px;
  margin: 5px 0;
}
button {
  width: 100%;
  padding: 10px;
  margin-top: 10px;
}
.error {
  color: red;
}
</style>
