import { jsx, jsxs } from "react/jsx-runtime";
import { useNavigate, Link } from "@tanstack/react-router";
import { useState, useEffect } from "react";
import { CircleCheckBig, ArrowRight, BadgeCheck, ShieldCheck } from "lucide-react";
import { s as supabase } from "./client-Dz5aErML.js";
import "@supabase/supabase-js";
function AdminSetup() {
  const navigate = useNavigate();
  const [checking, setChecking] = useState(true);
  const [hasAdmin, setHasAdmin] = useState(false);
  const [email] = useState("ozinpergam29@gmail.com");
  const [password, setPassword] = useState("Ozin 1234");
  const [submitting, setSubmitting] = useState(false);
  const [error, setError] = useState("");
  const [success, setSuccess] = useState(false);
  useEffect(() => {
    (async () => {
      const {
        count
      } = await supabase.from("user_roles").select("*", {
        count: "exact",
        head: true
      }).eq("role", "admin");
      setHasAdmin((count ?? 0) > 0);
      setChecking(false);
    })();
  }, []);
  const handleSubmit = async (e) => {
    e.preventDefault();
    setError("");
    setSubmitting(true);
    const {
      data,
      error: signErr
    } = await supabase.auth.signUp({
      email,
      password,
      options: {
        emailRedirectTo: `${window.location.origin}/admin/login`
      }
    });
    if (signErr || !data.user) {
      setSubmitting(false);
      setError(signErr?.message ?? "Gagal membuat akun");
      return;
    }
    const {
      error: roleErr
    } = await supabase.from("user_roles").insert({
      user_id: data.user.id,
      role: "admin"
    });
    setSubmitting(false);
    if (roleErr) {
      setError("Akun dibuat tapi role gagal disetel: " + roleErr.message);
      return;
    }
    setSuccess(true);
    setTimeout(() => navigate({
      to: "/admin/login"
    }), 2e3);
  };
  if (checking) {
    return /* @__PURE__ */ jsx("div", { className: "min-h-screen grid place-items-center text-sm text-muted-foreground", children: "Memeriksa..." });
  }
  if (hasAdmin) {
    return /* @__PURE__ */ jsx("div", { className: "min-h-screen grid place-items-center px-4", children: /* @__PURE__ */ jsxs("div", { className: "card-surface rounded-3xl p-8 max-w-md text-center", children: [
      /* @__PURE__ */ jsx(CircleCheckBig, { className: "mx-auto mb-3 h-11 w-11 text-primary/80", strokeWidth: 1.8 }),
      /* @__PURE__ */ jsx("h1", { className: "font-display font-bold text-xl", children: "Setup sudah selesai" }),
      /* @__PURE__ */ jsx("p", { className: "text-sm text-muted-foreground mt-2", children: "Admin sudah aktif di sistem. Silakan langsung login dengan email admin yang sudah ditentukan. Halaman setup ini terkunci untuk keamanan." }),
      /* @__PURE__ */ jsxs(Link, { to: "/admin/login", className: "group mt-5 inline-flex items-center gap-2 px-5 py-2.5 rounded-full bg-primary text-primary-foreground text-sm font-semibold", children: [
        "Ke Halaman Login",
        /* @__PURE__ */ jsx(ArrowRight, { className: "h-4 w-4 transition-transform group-hover:translate-x-0.5" })
      ] })
    ] }) });
  }
  if (success) {
    return /* @__PURE__ */ jsx("div", { className: "min-h-screen grid place-items-center px-4", children: /* @__PURE__ */ jsxs("div", { className: "card-surface rounded-3xl p-8 max-w-md text-center glow-purple-sm", children: [
      /* @__PURE__ */ jsx(BadgeCheck, { className: "mx-auto mb-3 h-11 w-11 text-primary/80", strokeWidth: 1.8 }),
      /* @__PURE__ */ jsx("h1", { className: "font-display font-bold text-xl", children: "Admin berhasil dibuat!" }),
      /* @__PURE__ */ jsx("p", { className: "text-sm text-muted-foreground mt-2", children: "Mengarahkan ke halaman login..." })
    ] }) });
  }
  return /* @__PURE__ */ jsx("div", { className: "min-h-screen grid place-items-center px-4 py-12", children: /* @__PURE__ */ jsxs("div", { className: "w-full max-w-md card-surface rounded-3xl p-8 glow-purple-sm", children: [
    /* @__PURE__ */ jsxs("div", { className: "text-center mb-6", children: [
      /* @__PURE__ */ jsx(ShieldCheck, { className: "mx-auto mb-2 h-10 w-10 text-primary/80", strokeWidth: 1.8 }),
      /* @__PURE__ */ jsx("h1", { className: "font-display text-2xl font-bold", children: "Setup Admin Pertama" }),
      /* @__PURE__ */ jsxs("p", { className: "text-sm text-muted-foreground mt-2", children: [
        "Buat akun admin pertama untuk mengelola Pergam Store. Email admin sudah dikunci ke",
        " ",
        /* @__PURE__ */ jsx("strong", { children: "ozinpergam29@gmail.com" }),
        ". Halaman ini akan terkunci otomatis setelah admin berhasil dibuat."
      ] })
    ] }),
    /* @__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, readOnly: true, className: "input mt-1 opacity-70 cursor-not-allowed" })
      ] }),
      /* @__PURE__ */ jsxs("div", { children: [
        /* @__PURE__ */ jsx("label", { className: "text-xs uppercase tracking-wider text-muted-foreground", children: "Password (min 6 karakter)" }),
        /* @__PURE__ */ jsx("input", { type: "password", required: true, minLength: 6, value: password, onChange: (e) => setPassword(e.target.value), className: "input mt-1" })
      ] }),
      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", { disabled: submitting, className: "w-full py-3 rounded-full bg-primary text-primary-foreground font-semibold glow-purple-sm disabled:opacity-60", children: submitting ? "Memproses..." : "Buat Admin & Mulai" })
    ] })
  ] }) });
}
export {
  AdminSetup as component
};
