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

const router = useRouter();
const route = useRoute();
const authStore = useAuthStore();

const username = ref("");
const password = ref("");
const error = ref("");
const submitting = ref(false);

const oauthError = computed(() => route.query.error === "oauth");

onMounted(async () => {
  await authStore.checkHasUsers();
  if (oauthError.value) {
    error.value = "SSO login failed. Please try again.";
  }
});

async function handleSubmit() {
  error.value = "";
  submitting.value = true;
  try {
    await authStore.login(username.value, password.value);
    const redirect = (route.query.redirect as string) || "/";
    router.push(redirect);
  } catch (e: unknown) {
    error.value = apiErrorMessage(e, "Login failed");
  } finally {
    submitting.value = false;
  }
}

function loginWithOAuth() {
  window.location.href = "/api/auth/oauth/login";
}
</script>

<template>
  <main class="auth-page">
    <div class="auth-card">
      <div class="auth-brand"><AppLogo :size="32" /><h1>Sign In</h1></div>
      <p v-if="!authStore.hasUsers" class="auth-hint">
        No accounts yet.
        <router-link to="/register">Create the first account</router-link>
        to get started.
      </p>

      <p v-if="error" class="error-msg">{{ error }}</p>

      <form v-if="authStore.localAuthEnabled" @submit.prevent="handleSubmit">
        <div class="field">
          <label for="username">Username</label>
          <input
            id="username"
            v-model="username"
            type="text"
            autocomplete="username"
            required
            class="input"
          />
        </div>
        <div class="field">
          <label for="password">Password</label>
          <input
            id="password"
            v-model="password"
            type="password"
            autocomplete="current-password"
            required
            class="input"
          />
        </div>
        <p class="forgot-link">
          <router-link to="/forgot-password">Forgot your password?</router-link>
        </p>
        <button type="submit" class="btn-primary btn-block" :disabled="submitting">
          {{ submitting ? "Signing in..." : "Sign In" }}
        </button>
      </form>

      <div
        v-if="authStore.localAuthEnabled && authStore.oauthEnabled"
        class="divider"
      >
        <span>or</span>
      </div>

      <button
        v-if="authStore.oauthEnabled"
        class="btn-ghost btn-block"
        @click="loginWithOAuth"
      >
        Login with Authentik
      </button>

      <p v-if="authStore.localAuthEnabled" class="auth-footer">
        Don't have an account?
        <router-link to="/register">Register</router-link>
      </p>
    </div>
  </main>
</template>

<style src="@/assets/auth-shared.css" />
<style scoped>
.divider {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  margin: 1.25rem 0;
  color: var(--fs-text-secondary);
  font-size: 0.85rem;
}
.divider::before,
.divider::after {
  content: "";
  flex: 1;
  border-top: 1px solid var(--fs-border-color);
}
.forgot-link {
  text-align: right;
  margin: -0.5rem 0 0.75rem;
  font-size: 0.85rem;
}
.forgot-link a {
  color: var(--fs-text-secondary);
}
.forgot-link a:hover {
  color: var(--fs-accent);
}
</style>
