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 BlogsAdmin() {
  const [rows, setRows] = useState([]);
  const [editing, setEditing] = useState(null);
  async function load() {
    const {
      data
    } = await supabase.from("blog_posts").select("id,title,slug,category,published").order("created_at", {
      ascending: false
    });
    setRows(data ?? []);
  }
  useEffect(() => {
    load();
  }, []);
  async function save(e) {
    e.preventDefault();
    if (!editing) return;
    const fd = new FormData(e.currentTarget);
    const payload = Object.fromEntries(fd.entries());
    payload.published = fd.get("published") === "on";
    const id = editing.id;
    const {
      error
    } = id ? await supabase.from("blog_posts").update(payload).eq("id", id) : await supabase.from("blog_posts").insert(payload);
    if (error) toast.error(error.message);
    else {
      toast.success("Saved");
      setEditing(null);
      load();
    }
  }
  async function remove(id) {
    if (!confirm("Delete this post?")) return;
    const {
      error
    } = await supabase.from("blog_posts").delete().eq("id", id);
    if (error) toast.error(error.message);
    else {
      toast.success("Deleted");
      load();
    }
  }
  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: "Blog Posts" }),
      /* @__PURE__ */ jsx("button", { onClick: () => setEditing({}), className: "bg-navy px-4 py-2 text-[10px] font-bold uppercase tracking-widest text-ivory hover:bg-gold hover:text-navy", children: "+ New Post" })
    ] }),
    /* @__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", children: /* @__PURE__ */ jsxs("tr", { children: [
        /* @__PURE__ */ jsx("th", { className: "px-4 py-3", children: "Title" }),
        /* @__PURE__ */ jsx("th", { className: "px-4 py-3", children: "Category" }),
        /* @__PURE__ */ jsx("th", { className: "px-4 py-3", children: "Status" }),
        /* @__PURE__ */ jsx("th", { className: "px-4 py-3" })
      ] }) }),
      /* @__PURE__ */ jsx("tbody", { children: rows.map((r) => /* @__PURE__ */ jsxs("tr", { className: "border-t border-navy/5", children: [
        /* @__PURE__ */ jsx("td", { className: "px-4 py-3 font-medium", children: r.title }),
        /* @__PURE__ */ jsx("td", { className: "px-4 py-3 text-xs", children: r.category }),
        /* @__PURE__ */ jsx("td", { className: "px-4 py-3 text-xs", children: r.published ? "Live" : "Draft" }),
        /* @__PURE__ */ jsxs("td", { className: "px-4 py-3 text-right", children: [
          /* @__PURE__ */ jsx("button", { onClick: () => setEditing(r), className: "text-xs underline", children: "Edit" }),
          /* @__PURE__ */ jsx("button", { onClick: () => remove(r.id), className: "ml-3 text-xs text-destructive underline", children: "Delete" })
        ] })
      ] }, r.id)) })
    ] }) }),
    editing && /* @__PURE__ */ jsx("div", { className: "fixed inset-0 z-50 grid place-items-center bg-navy/40 p-4", onClick: () => setEditing(null), children: /* @__PURE__ */ jsxs("form", { onSubmit: save, onClick: (e) => e.stopPropagation(), className: "max-h-[90vh] w-full max-w-2xl overflow-y-auto bg-white p-8", children: [
      /* @__PURE__ */ jsx("h2", { className: "font-display text-xl font-bold", children: editing.id ? "Edit Post" : "New Post" }),
      /* @__PURE__ */ jsxs("div", { className: "mt-6 grid gap-4", children: [
        /* @__PURE__ */ jsx(F, { name: "title", label: "Title", def: editing.title }),
        /* @__PURE__ */ jsx(F, { name: "slug", label: "Slug", def: editing.slug }),
        /* @__PURE__ */ jsx(F, { name: "category", label: "Category", def: editing.category }),
        /* @__PURE__ */ jsx(F, { name: "cover_image", label: "Cover Image URL", def: editing.cover_image }),
        /* @__PURE__ */ jsx(A, { name: "excerpt", label: "Excerpt", def: editing.excerpt, rows: 2 }),
        /* @__PURE__ */ jsx(A, { name: "content", label: "Content", def: editing.content, rows: 10 }),
        /* @__PURE__ */ jsx(F, { name: "meta_title", label: "SEO Title", def: editing.meta_title }),
        /* @__PURE__ */ jsx(F, { name: "meta_description", label: "SEO Description", def: editing.meta_description }),
        /* @__PURE__ */ jsxs("label", { className: "flex items-center gap-2 text-xs", children: [
          /* @__PURE__ */ jsx("input", { type: "checkbox", name: "published", defaultChecked: editing.published !== false }),
          " Published"
        ] })
      ] }),
      /* @__PURE__ */ jsxs("div", { className: "mt-6 flex justify-end gap-3", children: [
        /* @__PURE__ */ jsx("button", { type: "button", onClick: () => setEditing(null), className: "px-4 py-2 text-xs", children: "Cancel" }),
        /* @__PURE__ */ jsx("button", { type: "submit", className: "bg-navy px-6 py-2 text-[10px] font-bold uppercase tracking-widest text-ivory hover:bg-gold hover:text-navy", children: "Save" })
      ] })
    ] }) })
  ] });
}
function F({
  name,
  label,
  def
}) {
  return /* @__PURE__ */ jsxs("label", { className: "block", children: [
    /* @__PURE__ */ jsx("span", { className: "block text-[10px] uppercase tracking-widest text-navy/50", children: label }),
    /* @__PURE__ */ jsx("input", { name, defaultValue: def || "", className: "mt-1 w-full border-b border-navy/15 bg-transparent py-2 text-sm outline-none focus:border-gold" })
  ] });
}
function A({
  name,
  label,
  def,
  rows = 4
}) {
  return /* @__PURE__ */ jsxs("label", { className: "block", children: [
    /* @__PURE__ */ jsx("span", { className: "block text-[10px] uppercase tracking-widest text-navy/50", children: label }),
    /* @__PURE__ */ jsx("textarea", { name, defaultValue: def || "", rows, className: "mt-1 w-full border-b border-navy/15 bg-transparent py-2 text-sm outline-none focus:border-gold" })
  ] });
}
export {
  BlogsAdmin as component
};
