<script setup lang="ts">
import { ref, computed } from 'vue'
import { useRouter } from 'vue-router'
import { useAuthStore } from '@/stores/auth'
import Button from '@/components/common/Button.vue'

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

const username = ref('')
const password = ref('')
const isRegister = ref(false)
const loading = ref(false)
const error = ref('')

const formTitle = computed(() => isRegister.value ? 'Create Account' : 'Welcome Back')
const submitLabel = computed(() => isRegister.value ? 'Create Account' : 'Sign In')

async function handleSubmit() {
  if (!username.value || !password.value) {
    error.value = 'Please fill in all fields'
    return
  }

  loading.value = true
  error.value = ''

  try {
    if (isRegister.value) {
      await authStore.register(username.value, password.value)
      // After register, log in
      await authStore.login(username.value, password.value)
    } else {
      await authStore.login(username.value, password.value)
    }
    router.push('/dashboard')
  } catch (e) {
    error.value = e instanceof Error ? e.message : 'Authentication failed'
  } finally {
    loading.value = false
  }
}

function toggleMode() {
  isRegister.value = !isRegister.value
  error.value = ''
}
</script>

<template>
  <div class="login">
    <div class="login__card">
      <div class="login__header">
        <svg width="40" height="40" viewBox="0 0 28 28" fill="none">
          <rect width="28" height="28" rx="6" fill="var(--accent)"/>
          <path d="M8 10h12M8 14h8M8 18h10" stroke="white" stroke-width="2" stroke-linecap="round"/>
        </svg>
        <h1 class="login__title">{{ formTitle }}</h1>
        <p class="login__subtitle">
          {{ isRegister ? 'Create your account to get started' : 'Sign in to access your documents' }}
        </p>
      </div>

      <form class="login__form" @submit.prevent="handleSubmit">
        <div v-if="error" class="login__error">
          {{ error }}
        </div>

        <div class="login__field">
          <label for="username" class="login__label">Username</label>
          <input
            id="username"
            v-model="username"
            type="text"
            class="login__input"
            placeholder="Enter your username"
            autocomplete="username"
          />
        </div>

        <div class="login__field">
          <label for="password" class="login__label">Password</label>
          <input
            id="password"
            v-model="password"
            type="password"
            class="login__input"
            placeholder="Enter your password"
            autocomplete="current-password"
          />
        </div>

        <Button type="submit" variant="primary" size="lg" :loading="loading" style="width: 100%">
          {{ submitLabel }}
        </Button>

        <p class="login__toggle">
          {{ isRegister ? 'Already have an account?' : "Don't have an account?" }}
          <button type="button" class="login__toggle-btn" @click="toggleMode">
            {{ isRegister ? 'Sign in' : 'Create one' }}
          </button>
        </p>
      </form>
    </div>

    <p class="login__footer">
      Claudia Docs — Document Management for AI Agents
    </p>
  </div>
</template>

<style scoped>
.login {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 2rem;
  background: var(--bg-secondary);
}

.login__card {
  width: 100%;
  max-width: 400px;
  padding: 2.5rem;
  background: var(--bg-primary);
  border: 1px solid var(--border);
  border-radius: 16px;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.1);
}

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

.login__header svg {
  margin-bottom: 1rem;
}

.login__title {
  margin: 0;
  font-size: 1.5rem;
  font-weight: 600;
  color: var(--text-primary);
}

.login__subtitle {
  margin: 0.5rem 0 0;
  font-size: 0.9375rem;
  color: var(--text-secondary);
}

.login__form {
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
}

.login__error {
  padding: 0.75rem 1rem;
  background: #fef2f2;
  border: 1px solid #fecaca;
  border-radius: 8px;
  font-size: 0.875rem;
  color: #dc2626;
}

.login__field {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.login__label {
  font-size: 0.875rem;
  font-weight: 500;
  color: var(--text-primary);
}

.login__input {
  padding: 0.75rem 1rem;
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 0.9375rem;
  color: var(--text-primary);
  outline: none;
  transition: border-color 0.15s, box-shadow 0.15s;
}

.login__input::placeholder {
  color: var(--text-secondary);
}

.login__input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.1);
}

.login__toggle {
  margin: 0;
  text-align: center;
  font-size: 0.875rem;
  color: var(--text-secondary);
}

.login__toggle-btn {
  background: none;
  border: none;
  padding: 0;
  font-size: inherit;
  color: var(--accent);
  cursor: pointer;
  text-decoration: underline;
}

.login__toggle-btn:hover {
  color: var(--accent-hover);
}

.login__footer {
  margin-top: 2rem;
  font-size: 0.8125rem;
  color: var(--text-secondary);
}
</style>
