import { r as reactExports, U as jsxRuntimeExports } from "./worker-entry-DSg7Z9cD.js";
import { u as useAuth, L as Link, B as Button, s as supabase, t as toast } from "./router-CNfq_WjB.js";
import { I as Input } from "./input-CCsXbGDd.js";
import { C as Card } from "./card-ItD3O6C2.js";
import { A as ArrowLeft } from "./arrow-left-CSW_t4oE.js";
import { P as Plus } from "./plus-DEQvCTL0.js";
import { U as Upload } from "./upload-CscK_6ZM.js";
import { T as Trash2 } from "./trash-2-BomqsDeC.js";
import "node:events";
import "node:async_hooks";
import "node:stream/web";
import "node:stream";
function RecipientsPage() {
  const {
    user
  } = useAuth();
  const [items, setItems] = reactExports.useState([]);
  const [email, setEmail] = reactExports.useState("");
  const [name, setName] = reactExports.useState("");
  const [bulk, setBulk] = reactExports.useState("");
  const load = async () => {
    const {
      data
    } = await supabase.from("recipients").select("id,email,name").order("created_at", {
      ascending: false
    });
    setItems(data ?? []);
  };
  reactExports.useEffect(() => {
    if (user) load();
  }, [user]);
  const add = async (e) => {
    e.preventDefault();
    if (!user) return;
    const {
      error
    } = await supabase.from("recipients").insert({
      user_id: user.id,
      email: email.trim().toLowerCase(),
      name: name || null
    });
    if (error) return toast.error(error.message);
    setEmail("");
    setName("");
    load();
  };
  const addBulk = async () => {
    if (!user) return;
    const emails = bulk.split(/[\s,;\n]+/).map((e) => e.trim().toLowerCase()).filter((e) => /.+@.+\..+/.test(e));
    if (emails.length === 0) return toast.error("Aucun email valide détecté");
    const rows = emails.map((e) => ({
      user_id: user.id,
      email: e
    }));
    const {
      error
    } = await supabase.from("recipients").upsert(rows, {
      onConflict: "user_id,email",
      ignoreDuplicates: true
    });
    if (error) return toast.error(error.message);
    toast.success(`${emails.length} email(s) ajouté(s)`);
    setBulk("");
    load();
  };
  const remove = async (id) => {
    await supabase.from("recipients").delete().eq("id", id);
    load();
  };
  return /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: "mx-auto max-w-4xl 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: "Destinataires" }),
    /* @__PURE__ */ jsxRuntimeExports.jsx("p", { className: "mb-8 text-sm text-muted-foreground", children: "Gérez la liste des emails qui recevront vos communications." }),
    /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: "grid gap-6 md:grid-cols-2", children: [
      /* @__PURE__ */ jsxRuntimeExports.jsxs(Card, { className: "p-5", children: [
        /* @__PURE__ */ jsxRuntimeExports.jsx("h3", { className: "mb-3 font-semibold", children: "Ajouter un email" }),
        /* @__PURE__ */ jsxRuntimeExports.jsxs("form", { onSubmit: add, className: "space-y-3", children: [
          /* @__PURE__ */ jsxRuntimeExports.jsx(Input, { type: "email", placeholder: "email@exemple.com", required: true, value: email, onChange: (e) => setEmail(e.target.value) }),
          /* @__PURE__ */ jsxRuntimeExports.jsx(Input, { placeholder: "Nom (optionnel)", value: name, onChange: (e) => setName(e.target.value) }),
          /* @__PURE__ */ jsxRuntimeExports.jsxs(Button, { type: "submit", className: "w-full", children: [
            /* @__PURE__ */ jsxRuntimeExports.jsx(Plus, { className: "h-4 w-4" }),
            " Ajouter"
          ] })
        ] })
      ] }),
      /* @__PURE__ */ jsxRuntimeExports.jsxs(Card, { className: "p-5", children: [
        /* @__PURE__ */ jsxRuntimeExports.jsx("h3", { className: "mb-3 font-semibold", children: "Importer en masse" }),
        /* @__PURE__ */ jsxRuntimeExports.jsx("textarea", { className: "min-h-24 w-full rounded-md border bg-background p-3 text-sm", placeholder: "Collez vos emails séparés par des virgules, espaces ou retours à la ligne", value: bulk, onChange: (e) => setBulk(e.target.value) }),
        /* @__PURE__ */ jsxRuntimeExports.jsxs(Button, { onClick: addBulk, variant: "secondary", className: "mt-3 w-full", children: [
          /* @__PURE__ */ jsxRuntimeExports.jsx(Upload, { className: "h-4 w-4" }),
          " Importer"
        ] })
      ] })
    ] }),
    /* @__PURE__ */ jsxRuntimeExports.jsxs(Card, { className: "mt-6 p-5", children: [
      /* @__PURE__ */ jsxRuntimeExports.jsxs("h3", { className: "mb-3 font-semibold", children: [
        "Liste (",
        items.length,
        ")"
      ] }),
      items.length === 0 ? /* @__PURE__ */ jsxRuntimeExports.jsx("p", { className: "text-sm text-muted-foreground", children: "Aucun destinataire pour le moment." }) : /* @__PURE__ */ jsxRuntimeExports.jsx("div", { className: "divide-y", children: items.map((r) => /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: "flex items-center justify-between py-2.5", children: [
        /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { children: [
          /* @__PURE__ */ jsxRuntimeExports.jsx("div", { className: "text-sm font-medium", children: r.email }),
          r.name && /* @__PURE__ */ jsxRuntimeExports.jsx("div", { className: "text-xs text-muted-foreground", children: r.name })
        ] }),
        /* @__PURE__ */ jsxRuntimeExports.jsx(Button, { size: "sm", variant: "ghost", onClick: () => remove(r.id), children: /* @__PURE__ */ jsxRuntimeExports.jsx(Trash2, { className: "h-4 w-4 text-destructive" }) })
      ] }, r.id)) })
    ] })
  ] });
}
export {
  RecipientsPage as component
};
