import { jsx, jsxs } from "react/jsx-runtime";
import { useNavigate } from "@tanstack/react-router";
import { LockKeyhole, EyeOff, Eye, ArrowLeft } from "lucide-react";
import { useState, useEffect } from "react";
import { l as logo } from "./logo-DIdLvJAL.js";
import { s as supabase } from "./client-Dz5aErML.js";
import "@supabase/supabase-js";
const ALLOWED_ADMIN_EMAIL = "ozinpergam29@gmail.com";
async function verifyAdminSession(session) {
  if (!session?.user) return false;
  const emailAllowed = (session.user.email ?? "").toLowerCase() === ALLOWED_ADMIN_EMAIL.toLowerCase();
  if (!emailAllowed) return false;
  const {
    data
  } = await supabase.from("user_roles").select("role").eq("user_id", session.user.id).eq("role", "admin").maybeSingle();
  return !!data;
}
function AdminLogin() {
  const navigate = useNavigate();
  const [email, setEmail] = useState("");
  const [password, setPassword] = useState("");
  const [showPassword, setShowPassword] = useState(false);
  const [loading, setLoading] = useState(false);
  const [checkingSession, setCheckingSession] = useState(true);
  const [error, setError] = useState("");
  useEffect(() => {
    let active = true;
    supabase.auth.getSession().then(async ({
      data: {
        session
      }
    }) => {
      const isVerifiedAdmin = await verifyAdminSession(session);
      if (!active) return;
      if (isVerifiedAdmin) {
        navigate({
          to: "/adminpergam"
        });
      } else {
        if (session) await supabase.auth.signOut();
        setCheckingSession(false);
      }
    });
    return () => {
      active = false;
    };
  }, [navigate]);
  if (checkingSession) return null;
  const handleSubmit = async (e) => {
    e.preventDefault();
    setError("");
    setLoading(true);
    const {
      data,
      error: authError
    } = await supabase.auth.signInWithPassword({
      email,
      password
    });
    if (authError) {
      setLoading(false);
      setError(authError.message);
      return;
    }
    const isVerifiedAdmin = await verifyAdminSession(data.session);
    setLoading(false);
    if (!isVerifiedAdmin) {
      await supabase.auth.signOut();
      setError("Akun ini tidak memiliki izin admin.");
      return;
    }
    navigate({
      to: "/adminpergam"
    });
  };
  return /* @__PURE__ */ jsx("div", { className: "page-shell grid min-h-screen place-items-center px-4 py-12", children: /* @__PURE__ */ jsxs("div", { className: "w-full max-w-md rounded-[2rem] border border-border bg-card/80 p-5 shadow-card backdrop-blur-2xl sm:p-8", children: [
    /* @__PURE__ */ jsxs("div", { className: "mb-7 text-center", children: [
      /* @__PURE__ */ jsx("div", { className: "mx-auto mb-4 grid h-16 w-16 shrink-0 place-items-center overflow-hidden", children: /* @__PURE__ */ jsx("img", { src: logo, alt: "Pergam Store", className: "h-full w-full object-contain drop-shadow-sm" }) }),
      /* @__PURE__ */ jsxs("div", { className: "mx-auto mb-3 inline-flex items-center gap-2 rounded-full border border-primary/20 bg-primary/10 px-3 py-1 text-xs font-bold text-primary", children: [
        /* @__PURE__ */ jsx(LockKeyhole, { className: "h-3.5 w-3.5" }),
        "Secure Admin"
      ] }),
      /* @__PURE__ */ jsx("h1", { className: "font-display text-2xl font-bold text-foreground", children: "Admin Pergam Store" }),
      /* @__PURE__ */ jsx("p", { className: "mt-1 text-sm text-muted-foreground", children: "Masuk untuk mengelola katalog" })
    ] }),
    /* @__PURE__ */ jsxs("form", { onSubmit: handleSubmit, className: "space-y-4", children: [
      /* @__PURE__ */ jsxs("div", { children: [
        /* @__PURE__ */ jsx("label", { className: "text-xs uppercase tracking-wider text-muted-foreground", children: "Email" }),
        /* @__PURE__ */ jsx("input", { type: "email", required: true, value: email, onChange: (e) => setEmail(e.target.value), className: "input mt-1", placeholder: "admin@pergam.store" })
      ] }),
      /* @__PURE__ */ jsxs("div", { children: [
        /* @__PURE__ */ jsx("label", { className: "text-xs uppercase tracking-wider text-muted-foreground", children: "Password" }),
        /* @__PURE__ */ jsxs("div", { className: "relative mt-1", children: [
          /* @__PURE__ */ jsx("input", { type: showPassword ? "text" : "password", required: true, value: password, onChange: (e) => setPassword(e.target.value), className: "input pr-12", placeholder: "••••••••" }),
          /* @__PURE__ */ jsx("button", { type: "button", onClick: () => setShowPassword(!showPassword), className: "absolute right-3 top-1/2 -translate-y-1/2 p-1.5 text-muted-foreground hover:text-primary transition-colors focus:outline-none", "aria-label": showPassword ? "Hide password" : "Show password", children: showPassword ? /* @__PURE__ */ jsx(EyeOff, { className: "h-4 w-4" }) : /* @__PURE__ */ jsx(Eye, { className: "h-4 w-4" }) })
        ] })
      ] }),
      error && /* @__PURE__ */ jsx("div", { className: "text-sm text-destructive bg-destructive/10 border border-destructive/30 rounded-xl px-4 py-2", children: error }),
      /* @__PURE__ */ jsx("button", { type: "submit", disabled: loading, className: "btn-primary w-full disabled:opacity-60", children: loading ? "Memproses..." : "Masuk" })
    ] }),
    /* @__PURE__ */ jsx("div", { className: "mt-6 text-center", children: /* @__PURE__ */ jsxs("a", { href: "/", className: "inline-flex items-center gap-1.5 text-xs font-bold text-muted-foreground hover:text-foreground", children: [
      /* @__PURE__ */ jsx(ArrowLeft, { className: "h-3.5 w-3.5" }),
      "Kembali ke website"
    ] }) })
  ] }) });
}
export {
  AdminLogin as component
};
