<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>Set new password</h1>
        <p>Enter your new password below</p>
      </div>

      <form v-if="!success" @submit.prevent="handleSubmit" class="auth-form">
        <div class="form-group">
          <label for="password">New Password</label>
          <input
            id="password"
            v-model="password"
            type="password"
            placeholder="At least 8 characters"
            minlength="8"
            required
          />
        </div>

        <div class="form-group">
          <label for="confirmPassword">Confirm Password</label>
          <input
            id="confirmPassword"
            v-model="confirmPassword"
            type="password"
            placeholder="Repeat your password"
            required
          />
        </div>

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

        <button type="submit" class="cta full" :disabled="loading || !isValid">
          {{ loading ? 'Resetting...' : 'Reset password' }}
        </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>Password reset complete</h2>
        <p>Your password has been updated. You can now sign in with your new password.</p>
        <router-link to="/login" class="cta full">Sign in</router-link>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue';
import { useRoute } from 'vue-router';
import { authApi } from '../../api/auth.js';

const route = useRoute();

const password = ref('');
const confirmPassword = ref('');
const loading = ref(false);
const error = ref('');
const success = ref(false);

const token = computed(() => route.query.token || '');

const isValid = computed(() => {
  return password.value.length >= 8 && password.value === confirmPassword.value;
});

const handleSubmit = async () => {
  if (!token.value) {
    error.value = 'Invalid reset link. Please request a new one.';
    return;
  }

  if (password.value !== confirmPassword.value) {
    error.value = 'Passwords do not match';
    return;
  }

  loading.value = true;
  error.value = '';

  try {
    await authApi.resetPassword(token.value, password.value);
    success.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;
}
</style>
