<script setup lang="ts">
import { ref, computed } from "vue";
import { useRoute } from "vue-router";
import { apiPost, apiErrorMessage } from "@/api/client";
import AppLogo from "@/components/AppLogo.vue";

const route = useRoute();

const token = computed(() => (route.query.token as string) || "");
const password = ref("");
const confirmPassword = ref("");
const error = ref("");
const submitting = ref(false);
const success = ref(false);

const passwordMismatch = computed(
  () => confirmPassword.value.length > 0 && password.value !== confirmPassword.value
);

const canSubmit = computed(
  () => !submitting.value && !passwordMismatch.value && password.value.length >= 8 && !!token.value
);

async function handleSubmit() {
  if (passwordMismatch.value) {
    error.value = "Passwords do not match";
    return;
  }
  error.value = "";
  submitting.value = true;
  try {
    await apiPost("/api/auth/reset-password", {
      token: token.value,
      new_password: password.value,
    });
    success.value = true;
  } catch (e: unknown) {
    error.value = apiErrorMessage(e, "Failed to reset password");
  } finally {
    submitting.value = false;
  }
}
</script>

<template>
  <main class="auth-page">
    <div class="auth-card">
      <div class="auth-brand"><AppLogo :size="32" /><h1>Set New Password</h1></div>

      <div v-if="!token" class="auth-note">
        <p>Invalid reset link. Please request a new password reset.</p>
        <p class="auth-footer">
          <router-link to="/forgot-password">Request new link</router-link>
        </p>
      </div>

      <template v-else-if="!success">
        <form @submit.prevent="handleSubmit">
          <div class="field">
            <label for="password">New Password</label>
            <input
              id="password"
              v-model="password"
              type="password"
              autocomplete="new-password"
              required
              minlength="8"
              class="input"
            />
            <p class="field-hint">Must be at least 8 characters</p>
          </div>
          <div class="field">
            <label for="confirm-password">Confirm Password</label>
            <input
              id="confirm-password"
              v-model="confirmPassword"
              type="password"
              autocomplete="new-password"
              required
              class="input"
              :class="{ 'input-error': passwordMismatch }"
            />
            <p v-if="passwordMismatch" class="error-hint">Passwords do not match</p>
          </div>
          <p v-if="error" class="error-msg">{{ error }}</p>
          <button type="submit" class="btn-primary btn-block" :disabled="!canSubmit">
            {{ submitting ? "Resetting..." : "Reset Password" }}
          </button>
        </form>
      </template>

      <div v-else class="auth-note">
        <p>Your password has been reset successfully.</p>
        <p>You can now sign in with your new password.</p>
      </div>

      <p class="auth-footer">
        <router-link to="/login">Back to Sign In</router-link>
      </p>
    </div>
  </main>
</template>

<style src="@/assets/auth-shared.css" />
