import { jsxs, jsx } from "react/jsx-runtime";
import { useState, useEffect } from "react";
import { s as supabase } from "./client-P40JY-SL.js";
import { toast } from "sonner";
import "@supabase/supabase-js";
function LeadsPage() {
  const [rows, setRows] = useState([]);
  const [loading, setLoading] = useState(true);
  useEffect(() => {
    (async () => {
      const {
        data,
        error
      } = await supabase.from("leads").select("*").order("created_at", {
        ascending: false
      });
      if (error) toast.error(error.message);
      else setRows(data);
      setLoading(false);
    })();
  }, []);
  function exportCsv() {
    const headers = ["created_at", "full_name", "phone", "email", "budget", "property_interest", "preferred_location", "source_page", "message"];
    const csv = [headers.join(",")].concat(rows.map((r) => headers.map((h) => `"${String(r[h] ?? "").replace(/"/g, '""')}"`).join(","))).join("\n");
    const blob = new Blob([csv], {
      type: "text/csv"
    });
    const url = URL.createObjectURL(blob);
    const a = document.createElement("a");
    a.href = url;
    a.download = `bridgefield-leads-${(/* @__PURE__ */ new Date()).toISOString().slice(0, 10)}.csv`;
    a.click();
    URL.revokeObjectURL(url);
  }
  return /* @__PURE__ */ jsxs("div", { children: [
    /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-between", children: [
      /* @__PURE__ */ jsx("h1", { className: "font-display text-3xl font-bold", children: "Leads" }),
      /* @__PURE__ */ jsx("button", { onClick: exportCsv, className: "bg-navy px-4 py-2 text-[10px] font-bold uppercase tracking-widest text-ivory hover:bg-gold hover:text-navy", children: "Export CSV" })
    ] }),
    /* @__PURE__ */ jsx("div", { className: "mt-8 overflow-x-auto bg-white ring-1 ring-navy/10", children: /* @__PURE__ */ jsxs("table", { className: "min-w-full text-sm", children: [
      /* @__PURE__ */ jsx("thead", { className: "bg-navy/5 text-left text-[10px] uppercase tracking-widest text-navy/60", children: /* @__PURE__ */ jsxs("tr", { children: [
        /* @__PURE__ */ jsx("th", { className: "px-4 py-3", children: "Date" }),
        /* @__PURE__ */ jsx("th", { className: "px-4 py-3", children: "Name" }),
        /* @__PURE__ */ jsx("th", { className: "px-4 py-3", children: "Contact" }),
        /* @__PURE__ */ jsx("th", { className: "px-4 py-3", children: "Interest" }),
        /* @__PURE__ */ jsx("th", { className: "px-4 py-3", children: "Source" }),
        /* @__PURE__ */ jsx("th", { className: "px-4 py-3", children: "Message" })
      ] }) }),
      /* @__PURE__ */ jsx("tbody", { children: loading ? /* @__PURE__ */ jsx("tr", { children: /* @__PURE__ */ jsx("td", { colSpan: 6, className: "p-8 text-center text-navy/40", children: "Loading…" }) }) : rows.length === 0 ? /* @__PURE__ */ jsx("tr", { children: /* @__PURE__ */ jsx("td", { colSpan: 6, className: "p-8 text-center text-navy/40", children: "No leads yet." }) }) : rows.map((r) => /* @__PURE__ */ jsxs("tr", { className: "border-t border-navy/5 align-top", children: [
        /* @__PURE__ */ jsx("td", { className: "px-4 py-3 text-xs text-navy/60", children: new Date(r.created_at).toLocaleString() }),
        /* @__PURE__ */ jsx("td", { className: "px-4 py-3 font-medium", children: r.full_name }),
        /* @__PURE__ */ jsxs("td", { className: "px-4 py-3 text-xs", children: [
          /* @__PURE__ */ jsx("div", { children: r.phone }),
          /* @__PURE__ */ jsx("div", { className: "text-navy/60", children: r.email })
        ] }),
        /* @__PURE__ */ jsxs("td", { className: "px-4 py-3 text-xs", children: [
          /* @__PURE__ */ jsx("div", { children: r.property_interest || "—" }),
          /* @__PURE__ */ jsx("div", { className: "text-navy/60", children: r.budget || "" }),
          /* @__PURE__ */ jsx("div", { className: "text-navy/60", children: r.preferred_location || "" })
        ] }),
        /* @__PURE__ */ jsx("td", { className: "px-4 py-3 text-xs text-navy/60", children: r.source_page || "—" }),
        /* @__PURE__ */ jsx("td", { className: "px-4 py-3 max-w-xs text-xs text-navy/70", children: r.message || "—" })
      ] }, r.id)) })
    ] }) })
  ] });
}
export {
  LeadsPage as component
};
