import { jsx, jsxs } from "react/jsx-runtime";
import { useNavigate, Link } from "@tanstack/react-router";
import { useState } from "react";
import { s as supabase } from "./client-P40JY-SL.js";
import { toast } from "sonner";
import "@supabase/supabase-js";
function AuthPage() {
  const navigate = useNavigate();
  const [mode, setMode] = useState("signin");
  const [busy, setBusy] = useState(false);
  async function onSubmit(e) {
    e.preventDefault();
    setBusy(true);
    const fd = new FormData(e.currentTarget);
    const email = String(fd.get("email"));
    const password = String(fd.get("password"));
    try {
      if (mode === "signup") {
        const {
          error
        } = await supabase.auth.signUp({
          email,
          password,
          options: {
            emailRedirectTo: window.location.origin + "/admin"
          }
        });
        if (error) throw error;
        toast.success("Account created. Sign in to continue.");
        setMode("signin");
      } else {
        const {
          error
        } = await supabase.auth.signInWithPassword({
          email,
          password
        });
        if (error) throw error;
        navigate({
          to: "/admin"
        });
      }
    } catch (err) {
      toast.error(err instanceof Error ? err.message : "Authentication failed");
    } finally {
      setBusy(false);
    }
  }
  return /* @__PURE__ */ jsx("div", { className: "grid min-h-screen place-items-center bg-ivory px-4", children: /* @__PURE__ */ jsxs("div", { className: "w-full max-w-md bg-white p-8 ring-1 ring-navy/10", children: [
    /* @__PURE__ */ jsx(Link, { to: "/", className: "font-display text-2xl font-extrabold uppercase tracking-tighter", children: "Bridgefield" }),
    /* @__PURE__ */ jsx("h1", { className: "mt-6 font-display text-2xl font-bold", children: mode === "signin" ? "Admin Sign In" : "Create Admin Account" }),
    /* @__PURE__ */ jsxs("form", { onSubmit, className: "mt-6 space-y-4", children: [
      /* @__PURE__ */ jsx("input", { required: true, name: "email", type: "email", placeholder: "Email", className: "w-full border-b border-navy/15 bg-transparent py-2.5 text-sm outline-none focus:border-gold" }),
      /* @__PURE__ */ jsx("input", { required: true, name: "password", type: "password", placeholder: "Password", minLength: 6, className: "w-full border-b border-navy/15 bg-transparent py-2.5 text-sm outline-none focus:border-gold" }),
      /* @__PURE__ */ jsx("button", { type: "submit", disabled: busy, className: "w-full bg-navy py-4 text-[10px] font-bold uppercase tracking-widest text-ivory hover:bg-gold hover:text-navy disabled:opacity-60", children: busy ? "Please wait…" : mode === "signin" ? "Sign In" : "Sign Up" })
    ] }),
    /* @__PURE__ */ jsx("button", { onClick: () => setMode(mode === "signin" ? "signup" : "signin"), className: "mt-6 text-xs text-navy/60 underline", children: mode === "signin" ? "Need an account? Sign up" : "Have an account? Sign in" }),
    /* @__PURE__ */ jsx("p", { className: "mt-6 text-[10px] uppercase tracking-widest text-navy/40", children: "Admin role must be granted in the database after signup." })
  ] }) });
}
export {
  AuthPage as component
};
