<template>
  <div class="auth-page">
    <div class="auth-card">
      <div class="auth-header">
        <div class="brand">
          <span class="brand-mark">TQ</span>
          <span class="brand-name">TrakQR</span>
        </div>
        <h1>Reset your password</h1>
        <p>Enter your email and we'll send you a reset link</p>
      </div>

      <form v-if="!submitted" @submit.prevent="handleSubmit" class="auth-form">
        <div class="form-group">
          <label for="email">Email</label>
          <input
            id="email"
            v-model="email"
            type="email"
            placeholder="you@example.com"
            required
          />
        </div>

        <div v-if="error" class="error-message">
          {{ error }}
        </div>

        <button type="submit" class="cta full" :disabled="loading">
          {{ loading ? 'Sending...' : 'Send reset link' }}
        </button>
      </form>

      <div v-else class="success-message">
        <div class="success-icon">
          <svg width="48" height="48" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
            <path d="M22 11.08V12a10 10 0 1 1-5.93-9.14"/>
            <polyline points="22 4 12 14.01 9 11.01"/>
          </svg>
        </div>
        <h2>Check your email</h2>
        <p>If an account exists for {{ email }}, we've sent password reset instructions.</p>
        <router-link to="/login" class="cta full">Back to login</router-link>
      </div>

      <p class="auth-footer" v-if="!submitted">
        Remember your password?
        <router-link to="/login">Sign in</router-link>
      </p>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import { authApi } from '../../api/auth.js';

const email = ref('');
const loading = ref(false);
const error = ref('');
const submitted = ref(false);

const handleSubmit = async () => {
  loading.value = true;
  error.value = '';

  try {
    await authApi.forgotPassword(email.value);
    submitted.value = true;
  } catch (err) {
    error.value = err.message;
  } finally {
    loading.value = false;
  }
};
</script>

<style scoped>
.auth-page {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.auth-card {
  width: 100%;
  max-width: 400px;
  background: var(--surface);
  border-radius: 24px;
  padding: 40px;
  box-shadow: var(--shadow);
}

.auth-header {
  text-align: center;
  margin-bottom: 32px;
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 24px;
}

.brand-mark {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  border-radius: 12px;
  background: var(--ink);
  color: #fff4ec;
  font-weight: 700;
}

.brand-name {
  font-size: 1.2rem;
  font-weight: 600;
}

.auth-header h1 {
  font-size: 1.5rem;
  margin-bottom: 8px;
}

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

.auth-form {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.form-group label {
  font-weight: 500;
  font-size: 0.9rem;
}

.form-group input {
  padding: 12px 16px;
  border: 1px solid var(--line);
  border-radius: 12px;
  font-size: 1rem;
  font-family: inherit;
  transition: border-color 0.15s ease;
}

.form-group input:focus {
  outline: none;
  border-color: var(--accent);
}

.error-message {
  padding: 12px;
  background: #fef2f2;
  border: 1px solid #fecaca;
  border-radius: 10px;
  color: #dc2626;
  font-size: 0.9rem;
}

.success-message {
  text-align: center;
}

.success-icon {
  color: #22c55e;
  margin-bottom: 16px;
}

.success-message h2 {
  margin-bottom: 12px;
}

.success-message p {
  color: var(--muted);
  margin-bottom: 24px;
}

.cta.full {
  width: 100%;
  padding: 14px;
  font-size: 1rem;
  display: block;
  text-align: center;
}

.cta:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.auth-footer {
  text-align: center;
  margin-top: 24px;
  color: var(--muted);
}

.auth-footer a {
  color: var(--accent);
  font-weight: 500;
}
</style>
