<template>
  <div class="setup-container">
    <div class="setup-card">
      <h1>Initial System Setup</h1>
      <p>Welcome to the Student Council Election system. Please set up the initial credentials to proceed.</p>
      
      <form @submit.prevent="handleSetup">
        <div class="form-section">
          <h3>Admin Credentials</h3>
          <div class="form-group">
            <label>Admin Username</label>
            <input v-model="form.adminUser" type="text" placeholder="e.g. admin" required />
          </div>
          <div class="form-group">
            <label>Admin Password</label>
            <input v-model="form.adminPass" type="password" placeholder="Set a strong password" required />
          </div>
        </div>

        <div class="form-section">
          <h3>Booth Credentials</h3>
          <p class="section-desc">These credentials will be used at the voting stations.</p>
          <div class="form-group">
            <label>Booth Username</label>
            <input v-model="form.boothUser" type="text" placeholder="e.g. booth1" required />
          </div>
          <div class="form-group">
            <label>Booth Password</label>
            <input v-model="form.boothPass" type="password" placeholder="Set booth password" required />
          </div>
        </div>

        <div v-if="error" class="error-message">{{ error }}</div>
        
        <button type="submit" :disabled="loading" class="setup-btn">
          {{ loading ? 'Configuring System...' : 'Complete Setup' }}
        </button>
      </form>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import { authService } from '../services/api';

const router = useRouter();
const loading = ref(false);
const error = ref('');

const form = ref({
  adminUser: '',
  adminPass: '',
  boothUser: '',
  boothPass: ''
});

const handleSetup = async () => {
  loading.value = true;
  error.value = '';
  try {
    await authService.setup(form.value);
    alert('System configured successfully! Please login with your new credentials.');
    router.push('/');
  } catch (err) {
    error.value = err.response?.data?.error || 'Setup failed. Please try again.';
  } finally {
    loading.value = false;
  }
};
</script>

<style scoped>
.setup-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 80vh;
  padding: 2rem;
}

.setup-card {
  background: white;
  padding: 2.5rem;
  border-radius: 12px;
  box-shadow: 0 10px 25px rgba(0,0,0,0.1);
  max-width: 500px;
  width: 100%;
}

h1 {
  margin-bottom: 0.5rem;
  color: #2c3e50;
  text-align: center;
}

p {
  color: #666;
  text-align: center;
  margin-bottom: 2rem;
}

.form-section {
  margin-bottom: 2rem;
  padding: 1.5rem;
  background: #f8f9fa;
  border-radius: 8px;
  border: 1px solid #e9ecef;
}

h3 {
  margin-top: 0;
  margin-bottom: 1rem;
  font-size: 1.1rem;
  color: #34495e;
}

.section-desc {
  font-size: 0.85rem;
  text-align: left;
  margin-bottom: 1rem;
}

.form-group {
  margin-bottom: 1rem;
}

label {
  display: block;
  margin-bottom: 0.4rem;
  font-weight: 500;
  font-size: 0.9rem;
}

input {
  width: 100%;
  padding: 0.8rem;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 1rem;
  box-sizing: border-box;
}

input:focus {
  outline: none;
  border-color: #3498db;
  box-shadow: 0 0 0 2px rgba(52,152,219,0.2);
}

.setup-btn {
  width: 100%;
  padding: 1rem;
  background: #3498db;
  color: white;
  border: none;
  border-radius: 6px;
  font-size: 1.1rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s;
}

.setup-btn:hover {
  background: #2980b9;
}

.setup-btn:disabled {
  background: #bdc3c7;
  cursor: not-allowed;
}

.error-message {
  color: #e74c3c;
  background: #fdeaea;
  padding: 0.8rem;
  border-radius: 4px;
  margin-bottom: 1.5rem;
  font-size: 0.9rem;
  text-align: center;
}
</style>
