import { r as reactExports, U as jsxRuntimeExports } from "./worker-entry-DSg7Z9cD.js";
import { a as useNavigate, s as supabase, L as Link, B as Button, t as toast } from "./router-CNfq_WjB.js";
import { u as useIsAdmin } from "./useIsAdmin-Di1gULMM.js";
import { I as Input } from "./input-CCsXbGDd.js";
import { L as Label } from "./label-ByiRMO2o.js";
import { C as Card } from "./card-ItD3O6C2.js";
import { A as ArrowLeft } from "./arrow-left-CSW_t4oE.js";
import { S as Save } from "./save-aCyIokqt.js";
import "node:events";
import "node:async_hooks";
import "node:stream/web";
import "node:stream";
function AdminEmailPage() {
  const isAdmin = useIsAdmin();
  const navigate = useNavigate();
  const [host, setHost] = reactExports.useState("");
  const [port, setPort] = reactExports.useState(465);
  const [usr, setUsr] = reactExports.useState("");
  const [pass, setPass] = reactExports.useState("");
  const [fromName, setFromName] = reactExports.useState("");
  const [loading, setLoading] = reactExports.useState(true);
  const [saving, setSaving] = reactExports.useState(false);
  reactExports.useEffect(() => {
    if (isAdmin === false) navigate({
      to: "/app"
    });
  }, [isAdmin, navigate]);
  reactExports.useEffect(() => {
    if (!isAdmin) return;
    supabase.from("email_settings").select("*").eq("id", 1).maybeSingle().then(({
      data
    }) => {
      const d = data;
      if (d) {
        setHost(d.email_host ?? "");
        setPort(d.email_port ?? 465);
        setUsr(d.email_user ?? "");
        setPass(d.email_pass ?? "");
        setFromName(d.from_name ?? "");
      }
      setLoading(false);
    });
  }, [isAdmin]);
  const save = async () => {
    setSaving(true);
    const {
      error
    } = await supabase.from("email_settings").update({
      email_host: host || null,
      email_port: port,
      email_user: usr || null,
      email_pass: pass || null,
      from_name: fromName || null,
      updated_at: (/* @__PURE__ */ new Date()).toISOString()
    }).eq("id", 1);
    setSaving(false);
    if (error) return toast.error(error.message);
    toast.success("Configuration enregistrée");
  };
  if (isAdmin === null || loading) return /* @__PURE__ */ jsxRuntimeExports.jsx("div", { className: "p-10 text-muted-foreground", children: "Chargement…" });
  if (!isAdmin) return null;
  return /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: "mx-auto max-w-2xl p-6 md:p-10", children: [
    /* @__PURE__ */ jsxRuntimeExports.jsxs(Link, { to: "/app", className: "mb-4 inline-flex items-center gap-1 text-sm text-muted-foreground hover:text-foreground", children: [
      /* @__PURE__ */ jsxRuntimeExports.jsx(ArrowLeft, { className: "h-4 w-4" }),
      " Retour"
    ] }),
    /* @__PURE__ */ jsxRuntimeExports.jsx("h1", { className: "mb-1 text-3xl font-bold", children: "Configuration Email" }),
    /* @__PURE__ */ jsxRuntimeExports.jsx("p", { className: "mb-8 text-sm text-muted-foreground", children: "Paramètres SMTP utilisés pour envoyer les communications. Laissez vide pour utiliser les variables système." }),
    /* @__PURE__ */ jsxRuntimeExports.jsxs(Card, { className: "space-y-4 p-6", children: [
      /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: "space-y-2", children: [
        /* @__PURE__ */ jsxRuntimeExports.jsx(Label, { children: "Nom de l'expéditeur" }),
        /* @__PURE__ */ jsxRuntimeExports.jsx(Input, { value: fromName, onChange: (e) => setFromName(e.target.value), placeholder: "Communications Studio" })
      ] }),
      /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: "space-y-2", children: [
        /* @__PURE__ */ jsxRuntimeExports.jsx(Label, { children: "EMAIL_HOST" }),
        /* @__PURE__ */ jsxRuntimeExports.jsx(Input, { value: host, onChange: (e) => setHost(e.target.value), placeholder: "smtp.gmail.com" })
      ] }),
      /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: "space-y-2", children: [
        /* @__PURE__ */ jsxRuntimeExports.jsx(Label, { children: "EMAIL_PORT" }),
        /* @__PURE__ */ jsxRuntimeExports.jsx(Input, { type: "number", value: port, onChange: (e) => setPort(parseInt(e.target.value || "465", 10)) })
      ] }),
      /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: "space-y-2", children: [
        /* @__PURE__ */ jsxRuntimeExports.jsx(Label, { children: "EMAIL_USER" }),
        /* @__PURE__ */ jsxRuntimeExports.jsx(Input, { type: "email", value: usr, onChange: (e) => setUsr(e.target.value), placeholder: "user@gmail.com" })
      ] }),
      /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: "space-y-2", children: [
        /* @__PURE__ */ jsxRuntimeExports.jsx(Label, { children: "EMAIL_PASS" }),
        /* @__PURE__ */ jsxRuntimeExports.jsx(Input, { type: "password", value: pass, onChange: (e) => setPass(e.target.value), placeholder: "mot de passe d'application" })
      ] }),
      /* @__PURE__ */ jsxRuntimeExports.jsxs(Button, { onClick: save, disabled: saving, className: "w-full", children: [
        /* @__PURE__ */ jsxRuntimeExports.jsx(Save, { className: "h-4 w-4" }),
        " ",
        saving ? "..." : "Enregistrer"
      ] })
    ] })
  ] });
}
export {
  AdminEmailPage as component
};
