<script setup lang="ts">
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { useAuthStore } from '@/stores/auth'

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

const email = ref('')
const password = ref('')

async function handleSubmit(): Promise<void> {
  const success = await authStore.login(email.value, password.value)
  if (success) {
    router.push('/')
  }
}
</script>

<template>
  <div class="login-page">
    <div class="login-card">
      <h1 class="login-card__title">Admin Login</h1>

      <form class="login-card__form" @submit.prevent="handleSubmit">
        <label class="login-card__field">
          <span class="login-card__label">Email</span>
          <input
            v-model="email"
            data-cy="login-email"
            type="email"
            class="login-card__input"
            autocomplete="email"
          />
        </label>

        <label class="login-card__field">
          <span class="login-card__label">Password</span>
          <input
            v-model="password"
            data-cy="login-password"
            type="password"
            class="login-card__input"
            autocomplete="current-password"
          />
        </label>

        <p
          v-if="authStore.loginError"
          data-cy="login-error"
          class="login-card__error"
        >
          {{ authStore.loginError }}
        </p>

        <button
          data-cy="login-submit"
          type="submit"
          class="login-card__submit"
          :disabled="authStore.isSubmitting"
        >
          {{ authStore.isSubmitting ? 'Signing in...' : 'Sign In' }}
        </button>
      </form>
    </div>
  </div>
</template>

<style scoped>
.login-page {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  background: var(--color-cream);
  padding: 1rem;
}

.login-card {
  background: var(--color-white);
  border: 1px solid var(--color-border);
  border-radius: 8px;
  padding: 2.5rem;
  width: 100%;
  max-width: 400px;
}

.login-card__title {
  font-family: var(--font-serif);
  font-size: 1.5rem;
  color: var(--color-slate);
  text-align: center;
  margin: 0 0 1.5rem;
}

.login-card__form {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.login-card__field {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}

.login-card__label {
  font-size: 0.875rem;
  color: var(--color-text-muted);
}

.login-card__input {
  padding: 0.625rem 0.75rem;
  border: 1px solid var(--color-border);
  border-radius: 4px;
  font-family: var(--font-sans);
  font-size: 0.9375rem;
  color: var(--color-text);
  background: var(--color-white);
  outline: none;
  transition: border-color 0.15s ease;
}

.login-card__input:focus {
  border-color: var(--color-olive);
}

.login-card__error {
  font-size: 0.875rem;
  color: #b33;
  margin: 0;
  padding: 0.5rem 0.75rem;
  background: #fef2f2;
  border: 1px solid #fecaca;
  border-radius: 4px;
}

.login-card__submit {
  padding: 0.75rem 1rem;
  background: var(--color-olive);
  color: var(--color-white);
  font-family: var(--font-sans);
  font-size: 0.9375rem;
  font-weight: 500;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  transition: background 0.15s ease;
  margin-top: 0.5rem;
}

.login-card__submit:hover:not(:disabled) {
  background: var(--color-slate);
}

.login-card__submit:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}
</style>
