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 ProjectsAdmin() {
  const [rows, setRows] = useState([]);
  const [editing, setEditing] = useState(null);
  async function load() {
    const {
      data
    } = await supabase.from("projects").select("id,name,slug,location,starting_price,published,featured").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.featured = fd.get("featured") === "on";
    payload.published = fd.get("published") === "on";
    payload.amenities = String(fd.get("amenities_csv") || "").split(",").map((s) => s.trim()).filter(Boolean);
    delete payload.amenities_csv;
    const id = editing.id;
    const {
      error
    } = id ? await supabase.from("projects").update(payload).eq("id", id) : await supabase.from("projects").insert(payload);
    if (error) toast.error(error.message);
    else {
      toast.success("Saved");
      setEditing(null);
      load();
    }
  }
  async function remove(id) {
    if (!confirm("Delete this project?")) return;
    const {
      error
    } = await supabase.from("projects").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: "Projects" }),
      /* @__PURE__ */ jsx("button", { onClick: () => setEditing({
        amenities: []
      }), className: "bg-navy px-4 py-2 text-[10px] font-bold uppercase tracking-widest text-ivory hover:bg-gold hover:text-navy", children: "+ New Project" })
    ] }),
    /* @__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: "Name" }),
        /* @__PURE__ */ jsx("th", { className: "px-4 py-3", children: "Location" }),
        /* @__PURE__ */ jsx("th", { className: "px-4 py-3", children: "From" }),
        /* @__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.name }),
        /* @__PURE__ */ jsx("td", { className: "px-4 py-3 text-xs text-navy/60", children: r.location }),
        /* @__PURE__ */ jsx("td", { className: "px-4 py-3 text-xs", children: r.starting_price }),
        /* @__PURE__ */ jsxs("td", { className: "px-4 py-3 text-xs", children: [
          r.featured ? "Featured · " : "",
          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 Project" : "New Project" }),
      /* @__PURE__ */ jsxs("div", { className: "mt-6 grid gap-4 sm:grid-cols-2", children: [
        /* @__PURE__ */ jsx(Field, { name: "name", label: "Name", def: editing.name }),
        /* @__PURE__ */ jsx(Field, { name: "slug", label: "Slug", def: editing.slug }),
        /* @__PURE__ */ jsx(Field, { name: "location", label: "Location", def: editing.location }),
        /* @__PURE__ */ jsx(Field, { name: "property_type", label: "Property Type", def: editing.property_type || "Townhouse" }),
        /* @__PURE__ */ jsx(Field, { name: "bedrooms", label: "Bedrooms", def: editing.bedrooms }),
        /* @__PURE__ */ jsx(Field, { name: "starting_price", label: "Starting Price", def: editing.starting_price }),
        /* @__PURE__ */ jsx(Field, { name: "payment_plan", label: "Payment Plan", def: editing.payment_plan }),
        /* @__PURE__ */ jsx(Field, { name: "handover_status", label: "Handover", def: editing.handover_status }),
        /* @__PURE__ */ jsx(Field, { name: "cover_image", label: "Cover Image URL", def: editing.cover_image, className: "sm:col-span-2" }),
        /* @__PURE__ */ jsx(Area, { name: "short_description", label: "Short Description", def: editing.short_description }),
        /* @__PURE__ */ jsx(Area, { name: "overview", label: "Overview", def: editing.overview }),
        /* @__PURE__ */ jsx(Field, { name: "amenities_csv", label: "Amenities (comma separated)", def: editing.amenities?.join(", ") || "", className: "sm:col-span-2" }),
        /* @__PURE__ */ jsx(Field, { name: "meta_title", label: "SEO Title", def: editing.meta_title, className: "sm:col-span-2" }),
        /* @__PURE__ */ jsx(Field, { name: "meta_description", label: "SEO Description", def: editing.meta_description, className: "sm:col-span-2" }),
        /* @__PURE__ */ jsxs("label", { className: "flex items-center gap-2 text-xs", children: [
          /* @__PURE__ */ jsx("input", { type: "checkbox", name: "featured", defaultChecked: !!editing.featured }),
          " Featured"
        ] }),
        /* @__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 Field({
  name,
  label,
  def,
  className = ""
}) {
  return /* @__PURE__ */ jsxs("label", { className: `block ${className}`, 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 Area({
  name,
  label,
  def
}) {
  return /* @__PURE__ */ jsxs("label", { className: "block sm:col-span-2", children: [
    /* @__PURE__ */ jsx("span", { className: "block text-[10px] uppercase tracking-widest text-navy/50", children: label }),
    /* @__PURE__ */ jsx("textarea", { name, defaultValue: def || "", rows: 3, className: "mt-1 w-full border-b border-navy/15 bg-transparent py-2 text-sm outline-none focus:border-gold" })
  ] });
}
export {
  ProjectsAdmin as component
};
