<script setup lang="ts">
const { signInWithEmail, signInWithGoogle, error } = useAuth();

const isOpen = useLoginModal();
const email = ref("");
const password = ref("");
const loginError = ref<string | null>(null);
const isSubmitting = ref(false);

const handleEmailLogin = async () => {
  try {
    loginError.value = null;
    isSubmitting.value = true;
    await signInWithEmail(email.value, password.value);
    isOpen.value = false;
    // Reset form
    email.value = "";
    password.value = "";
  } catch (err) {
    loginError.value = "Login failed. Please check your credentials.";
  } finally {
    isSubmitting.value = false;
  }
};

const handleGoogleLogin = async () => {
  try {
    loginError.value = null;
    isSubmitting.value = true;
    await signInWithGoogle();
    isOpen.value = false;
  } catch (err) {
    loginError.value = "Google login failed. Please try again.";
  } finally {
    isSubmitting.value = false;
  }
};
</script>

<template>
  <Teleport to="body">
    <div
      v-if="isOpen"
      class="fixed left-0 top-0 z-50 flex min-h-screen w-full items-center justify-center bg-black/60 px-4"
      @click.self="isOpen = false"
    >
      <div class="w-full max-w-md rounded-lg bg-white p-6 shadow-xl">
        <h2 class="mb-6 text-2xl font-bold text-slate-900">Sign In</h2>

        <form @submit.prevent="handleEmailLogin" class="space-y-4">
          <div>
            <label class="block text-sm font-medium text-slate-700"
              >Email</label
            >
            <input
              v-model="email"
              type="email"
              required
              class="mt-1 w-full rounded-md border border-slate-300 bg-white px-3 py-2 text-slate-900 focus:border-slate-900 focus:outline-none focus:ring-2 focus:ring-slate-900/20"
            />
          </div>

          <div>
            <label class="block text-sm font-medium text-slate-700"
              >Password</label
            >
            <input
              v-model="password"
              type="password"
              required
              class="mt-1 w-full rounded-md border border-slate-300 bg-white px-3 py-2 text-slate-900 focus:border-slate-900 focus:outline-none focus:ring-2 focus:ring-slate-900/20"
            />
          </div>

          <button
            type="submit"
            :disabled="isSubmitting"
            class="w-full rounded-md border-2 border-slate-900 bg-slate-900 px-4 py-2 text-white transition hover:bg-white hover:text-slate-900 disabled:opacity-50"
          >
            {{ isSubmitting ? "Signing in..." : "Sign In" }}
          </button>
        </form>

        <div class="my-4 flex items-center">
          <div class="flex-1 border-t border-slate-200"></div>
          <span class="px-3 text-sm text-slate-500">or</span>
          <div class="flex-1 border-t border-slate-200"></div>
        </div>

        <button
          @click="handleGoogleLogin"
          :disabled="isSubmitting"
          class="w-full rounded-md border border-slate-300 bg-white px-4 py-2 text-slate-700 transition hover:bg-slate-50 disabled:opacity-50"
        >
          Sign in with Google
        </button>

        <div v-if="loginError || error" class="mt-4 rounded-md border border-rose-200 bg-rose-50 px-3 py-2 text-sm text-rose-700">
          {{ loginError || error }}
        </div>

        <p class="mt-4 text-sm text-slate-600">
          Need an account?
          <NuxtLink
            to="/register"
            class="font-semibold text-slate-900 hover:text-slate-700"
            @click="isOpen = false"
          >
            Create one instead
          </NuxtLink>
        </p>

        <button
          @click="isOpen = false"
          class="mt-4 text-sm text-slate-600 hover:text-slate-900"
        >
          Cancel
        </button>
      </div>
    </div>
  </Teleport>
</template>
