import { S as reactExports, J as jsxRuntimeExports } from "./server-BiCSmoPr.js";
import { h as createLucideIcon, L as Link, p as motion, b as INVESTOR_DOCUMENT_EXTENSIONS, a as INVESTOR_DOCUMENT_CATEGORIES } from "./router-K1srMTuI.js";
import { A as ArrowLeft } from "./arrow-left-DOE61fAZ.js";
import { U as Upload, E as Eye } from "./upload-BWmUxVIq.js";
import { F as FileText } from "./file-text-CbBXWTmN.js";
import "node:async_hooks";
import "node:stream/web";
import "node:stream";
const __iconNode$2 = [
  [
    "path",
    {
      d: "M10.733 5.076a10.744 10.744 0 0 1 11.205 6.575 1 1 0 0 1 0 .696 10.747 10.747 0 0 1-1.444 2.49",
      key: "ct8e1f"
    }
  ],
  ["path", { d: "M14.084 14.158a3 3 0 0 1-4.242-4.242", key: "151rxh" }],
  [
    "path",
    {
      d: "M17.479 17.499a10.75 10.75 0 0 1-15.417-5.151 1 1 0 0 1 0-.696 10.75 10.75 0 0 1 4.446-5.143",
      key: "13bj9a"
    }
  ],
  ["path", { d: "m2 2 20 20", key: "1ooewy" }]
];
const EyeOff = createLucideIcon("eye-off", __iconNode$2);
const __iconNode$1 = [
  [
    "path",
    {
      d: "M20 13c0 5-3.5 7.5-7.66 8.95a1 1 0 0 1-.67-.01C7.5 20.5 4 18 4 13V6a1 1 0 0 1 1-1c2 0 4.5-1.2 6.24-2.72a1.17 1.17 0 0 1 1.52 0C14.51 3.81 17 5 19 5a1 1 0 0 1 1 1z",
      key: "oel41y"
    }
  ]
];
const Shield = createLucideIcon("shield", __iconNode$1);
const __iconNode = [
  ["path", { d: "M10 11v6", key: "nco0om" }],
  ["path", { d: "M14 11v6", key: "outv1u" }],
  ["path", { d: "M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6", key: "miytrc" }],
  ["path", { d: "M3 6h18", key: "d0wm0j" }],
  ["path", { d: "M8 6V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2", key: "e791ji" }]
];
const Trash2 = createLucideIcon("trash-2", __iconNode);
const uploadCategories = INVESTOR_DOCUMENT_CATEGORIES.filter((category) => category !== "All");
const DEFAULT_UPLOAD_CATEGORY = "Company Profile";
function InvestorDeskAdmin() {
  const [adminKey, setAdminKey] = reactExports.useState("");
  const [isUnlocked, setIsUnlocked] = reactExports.useState(false);
  const [documents, setDocuments] = reactExports.useState([]);
  const [title, setTitle] = reactExports.useState("");
  const [category, setCategory] = reactExports.useState(DEFAULT_UPLOAD_CATEGORY);
  const [description, setDescription] = reactExports.useState("");
  const [visibility, setVisibility] = reactExports.useState("public");
  const [file, setFile] = reactExports.useState(null);
  const [status, setStatus] = reactExports.useState("");
  const [error, setError] = reactExports.useState("");
  const [isSubmitting, setIsSubmitting] = reactExports.useState(false);
  const uploadedDocuments = reactExports.useMemo(() => documents.filter((document) => document.id.startsWith("upload-")), [documents]);
  reactExports.useEffect(() => {
    setAdminKey(sessionStorage.getItem("sr18_admin_upload_key") ?? "");
  }, []);
  function loadDocuments(key = adminKey) {
    fetch("/api/investor-documents", {
      headers: {
        "x-admin-key": key.trim()
      }
    }).then((response) => response.json()).then((data) => setDocuments(data.documents)).catch(() => setError("Unable to load investor documents."));
  }
  function unlockAdmin(event) {
    event.preventDefault();
    setError("");
    setStatus("");
    const key = adminKey.trim();
    if (!key) {
      setError("Admin key is required.");
      return;
    }
    sessionStorage.setItem("sr18_admin_upload_key", key);
    setIsUnlocked(true);
    loadDocuments(key);
  }
  async function uploadDocument(event) {
    event.preventDefault();
    const form = event.currentTarget;
    setError("");
    setStatus("");
    if (!file) {
      setError("Choose a document to upload.");
      return;
    }
    const formData = new FormData();
    formData.set("title", title);
    formData.set("category", category);
    formData.set("description", description);
    formData.set("visibility", visibility);
    formData.set("file", file);
    setIsSubmitting(true);
    try {
      const response = await fetch("/api/investor-documents", {
        method: "POST",
        headers: {
          "x-admin-key": adminKey.trim()
        },
        body: formData
      });
      const data = await response.json();
      if (!response.ok || !data.document) throw new Error(data.error || "Upload failed.");
      setDocuments((current) => [data.document, ...current]);
      setTitle("");
      setDescription("");
      setVisibility("public");
      setFile(null);
      setStatus("Document uploaded to Investor Desk.");
      form.reset();
      setCategory(DEFAULT_UPLOAD_CATEGORY);
    } catch (err) {
      setError(err instanceof Error ? err.message : "Upload failed.");
    } finally {
      setIsSubmitting(false);
    }
  }
  async function deleteDocument(id) {
    setError("");
    setStatus("");
    const response = await fetch(`/api/investor-documents?id=${encodeURIComponent(id)}`, {
      method: "DELETE",
      headers: {
        "x-admin-key": adminKey.trim()
      }
    });
    if (!response.ok) {
      const data = await response.json().catch(() => ({}));
      setError(data.error || "Delete failed.");
      return;
    }
    setDocuments((current) => current.filter((document) => document.id !== id));
    setStatus("Document removed from Investor Desk.");
  }
  return /* @__PURE__ */ jsxRuntimeExports.jsx("main", { className: "min-h-screen bg-bone px-6 pb-24 pt-32", children: /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: "mx-auto max-w-6xl", children: [
    /* @__PURE__ */ jsxRuntimeExports.jsxs(Link, { to: "/investor-desk", className: "mb-10 inline-flex items-center gap-2 text-[10px] uppercase tracking-[0.3em] text-muted-foreground transition-colors hover:text-foreground", children: [
      /* @__PURE__ */ jsxRuntimeExports.jsx(ArrowLeft, { className: "h-3 w-3" }),
      " Back to Investor Desk"
    ] }),
    /* @__PURE__ */ jsxRuntimeExports.jsxs(motion.div, { initial: {
      opacity: 0,
      y: 18
    }, animate: {
      opacity: 1,
      y: 0
    }, transition: {
      duration: 0.7
    }, className: "mb-10", children: [
      /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: "mb-6 flex items-center gap-3 text-[10px] uppercase tracking-[0.35em] text-muted-foreground", children: [
        /* @__PURE__ */ jsxRuntimeExports.jsx(Shield, { className: "h-4 w-4 text-gold" }),
        /* @__PURE__ */ jsxRuntimeExports.jsx("span", { children: "Admin Only" })
      ] }),
      /* @__PURE__ */ jsxRuntimeExports.jsx("h1", { className: "text-display max-w-4xl text-[clamp(2.2rem,5vw,4.5rem)] leading-[1]", children: "Investor Desk document CMS." })
    ] }),
    !isUnlocked ? /* @__PURE__ */ jsxRuntimeExports.jsxs("form", { onSubmit: unlockAdmin, className: "max-w-xl border border-border bg-background p-7", children: [
      /* @__PURE__ */ jsxRuntimeExports.jsx("label", { className: "mb-3 block text-[10px] uppercase tracking-[0.25em] text-muted-foreground", children: "Admin key" }),
      /* @__PURE__ */ jsxRuntimeExports.jsx("input", { value: adminKey, onChange: (event) => setAdminKey(event.target.value), type: "password", className: "w-full bg-mist px-4 py-3 text-sm outline-none focus:ring-1 focus:ring-gold", autoComplete: "current-password" }),
      error && /* @__PURE__ */ jsxRuntimeExports.jsx("p", { className: "mt-4 text-sm text-destructive", children: error }),
      /* @__PURE__ */ jsxRuntimeExports.jsxs("button", { className: "mt-6 inline-flex items-center gap-3 bg-foreground px-6 py-3 text-xs uppercase tracking-[0.25em] text-background transition-colors hover:bg-gold hover:text-foreground", children: [
        /* @__PURE__ */ jsxRuntimeExports.jsx(Shield, { className: "h-4 w-4" }),
        " Unlock"
      ] })
    ] }) : /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: "grid gap-8 lg:grid-cols-[0.9fr_1.1fr]", children: [
      /* @__PURE__ */ jsxRuntimeExports.jsxs("form", { onSubmit: uploadDocument, className: "border border-border bg-background p-7", children: [
        /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: "mb-6 flex items-center gap-3 text-[10px] uppercase tracking-[0.3em] text-gold", children: [
          /* @__PURE__ */ jsxRuntimeExports.jsx(Upload, { className: "h-4 w-4" }),
          "Upload Document"
        ] }),
        /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: "space-y-5", children: [
          /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { children: [
            /* @__PURE__ */ jsxRuntimeExports.jsx("label", { className: "mb-2 block text-[10px] uppercase tracking-[0.25em] text-muted-foreground", children: "Document title" }),
            /* @__PURE__ */ jsxRuntimeExports.jsx("input", { value: title, onChange: (event) => setTitle(event.target.value), required: true, className: "w-full bg-mist px-4 py-3 text-sm outline-none focus:ring-1 focus:ring-gold" })
          ] }),
          /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { children: [
            /* @__PURE__ */ jsxRuntimeExports.jsx("label", { className: "mb-2 block text-[10px] uppercase tracking-[0.25em] text-muted-foreground", children: "Category" }),
            /* @__PURE__ */ jsxRuntimeExports.jsx("select", { value: category, onChange: (event) => setCategory(event.target.value), className: "w-full bg-mist px-4 py-3 text-sm outline-none focus:ring-1 focus:ring-gold", children: uploadCategories.map((item) => /* @__PURE__ */ jsxRuntimeExports.jsx("option", { children: item }, item)) })
          ] }),
          /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { children: [
            /* @__PURE__ */ jsxRuntimeExports.jsx("label", { className: "mb-2 block text-[10px] uppercase tracking-[0.25em] text-muted-foreground", children: "Short description" }),
            /* @__PURE__ */ jsxRuntimeExports.jsx("textarea", { value: description, onChange: (event) => setDescription(event.target.value), required: true, rows: 4, className: "w-full resize-none bg-mist px-4 py-3 text-sm outline-none focus:ring-1 focus:ring-gold" })
          ] }),
          /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { children: [
            /* @__PURE__ */ jsxRuntimeExports.jsx("label", { className: "mb-2 block text-[10px] uppercase tracking-[0.25em] text-muted-foreground", children: "Visibility" }),
            /* @__PURE__ */ jsxRuntimeExports.jsx("div", { className: "grid grid-cols-2 gap-2", children: ["public", "private"].map((item) => /* @__PURE__ */ jsxRuntimeExports.jsxs("button", { type: "button", onClick: () => setVisibility(item), className: `flex items-center justify-center gap-2 border px-4 py-3 text-[10px] uppercase tracking-[0.25em] transition-colors ${visibility === item ? "border-foreground bg-foreground text-background" : "border-border bg-mist hover:border-foreground"}`, children: [
              item === "public" ? /* @__PURE__ */ jsxRuntimeExports.jsx(Eye, { className: "h-4 w-4" }) : /* @__PURE__ */ jsxRuntimeExports.jsx(EyeOff, { className: "h-4 w-4" }),
              item
            ] }, item)) })
          ] }),
          /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { children: [
            /* @__PURE__ */ jsxRuntimeExports.jsx("label", { className: "mb-2 block text-[10px] uppercase tracking-[0.25em] text-muted-foreground", children: "File" }),
            /* @__PURE__ */ jsxRuntimeExports.jsx("input", { onChange: (event) => setFile(event.target.files?.[0] ?? null), required: true, accept: INVESTOR_DOCUMENT_EXTENSIONS.map((extension) => `.${extension}`).join(","), type: "file", className: "w-full bg-mist px-4 py-3 text-sm outline-none focus:ring-1 focus:ring-gold" }),
            /* @__PURE__ */ jsxRuntimeExports.jsxs("p", { className: "mt-2 text-xs text-muted-foreground", children: [
              INVESTOR_DOCUMENT_EXTENSIONS.join(", ").toUpperCase(),
              " files up to 30 MB."
            ] })
          ] })
        ] }),
        error && /* @__PURE__ */ jsxRuntimeExports.jsx("p", { className: "mt-5 text-sm text-destructive", children: error }),
        status && /* @__PURE__ */ jsxRuntimeExports.jsx("p", { className: "mt-5 text-sm text-muted-foreground", children: status }),
        /* @__PURE__ */ jsxRuntimeExports.jsxs("button", { disabled: isSubmitting, className: "mt-7 inline-flex items-center gap-3 bg-foreground px-6 py-3 text-xs uppercase tracking-[0.25em] text-background transition-colors hover:bg-gold hover:text-foreground disabled:opacity-50", children: [
          /* @__PURE__ */ jsxRuntimeExports.jsx(Upload, { className: "h-4 w-4" }),
          " ",
          isSubmitting ? "Uploading" : "Upload Document"
        ] })
      ] }),
      /* @__PURE__ */ jsxRuntimeExports.jsxs("section", { className: "border border-border bg-background p-7", children: [
        /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: "mb-6 flex items-center justify-between gap-4", children: [
          /* @__PURE__ */ jsxRuntimeExports.jsx("div", { className: "text-[10px] uppercase tracking-[0.3em] text-gold", children: "Document Library" }),
          /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: "text-[10px] uppercase tracking-[0.25em] text-muted-foreground", children: [
            uploadedDocuments.length,
            " files"
          ] })
        ] }),
        /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: "divide-y divide-border", children: [
          uploadedDocuments.map((document) => /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: "flex flex-col gap-5 py-5 sm:flex-row sm:items-start sm:justify-between", children: [
            /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: "flex gap-4", children: [
              /* @__PURE__ */ jsxRuntimeExports.jsx("div", { className: "flex h-10 w-10 shrink-0 items-center justify-center bg-mist", children: /* @__PURE__ */ jsxRuntimeExports.jsx(FileText, { className: "h-4 w-4" }) }),
              /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { children: [
                /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: "flex flex-wrap items-center gap-2", children: [
                  /* @__PURE__ */ jsxRuntimeExports.jsx("h2", { className: "text-display text-xl leading-tight", children: document.title }),
                  /* @__PURE__ */ jsxRuntimeExports.jsx("span", { className: "border border-border px-2 py-1 text-[9px] uppercase tracking-[0.2em] text-muted-foreground", children: document.visibility })
                ] }),
                /* @__PURE__ */ jsxRuntimeExports.jsx("p", { className: "mt-2 max-w-xl text-sm leading-relaxed text-muted-foreground", children: document.description }),
                /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: "mt-2 text-[10px] uppercase tracking-[0.25em] text-muted-foreground", children: [
                  document.category,
                  " · ",
                  document.date,
                  " · ",
                  document.size,
                  " · ",
                  document.type
                ] })
              ] })
            ] }),
            /* @__PURE__ */ jsxRuntimeExports.jsx("button", { onClick: () => deleteDocument(document.id), className: "flex h-10 w-10 shrink-0 items-center justify-center bg-mist transition-colors hover:bg-foreground hover:text-background", "aria-label": `Delete ${document.title}`, children: /* @__PURE__ */ jsxRuntimeExports.jsx(Trash2, { className: "h-4 w-4" }) })
          ] }, document.id)),
          uploadedDocuments.length === 0 && /* @__PURE__ */ jsxRuntimeExports.jsx("div", { className: "py-12 text-sm text-muted-foreground", children: "No uploaded documents yet." })
        ] })
      ] })
    ] })
  ] }) });
}
export {
  InvestorDeskAdmin as component
};
