import { jsxs, jsx, Fragment } from "react/jsx-runtime";
import { useQueryClient, useQuery, useMutation } from "@tanstack/react-query";
import { useState } from "react";
import { s as supabase } from "./client-CJCZGN4z.js";
import { A as AdminShell } from "./AdminShell-fo83oaxY.js";
import { D as DeleteButton } from "./DeleteButton-D7V3t-jE.js";
import { B as Button } from "./button-BXrfXN_b.js";
import { L as Label, I as Input } from "./label-BsuFFEVs.js";
import { S as Switch } from "./switch-DDHih_sy.js";
import { D as Dialog, a as DialogContent, b as DialogHeader, c as DialogTitle, d as DialogFooter } from "./alert-dialog-Cbc2KPWf.js";
import { Plus, Edit } from "lucide-react";
import { toast } from "sonner";
import "@supabase/supabase-js";
import "@tanstack/react-router";
import "./use-auth-DoD1gvcu.js";
import "@radix-ui/react-slot";
import "class-variance-authority";
import "clsx";
import "tailwind-merge";
import "@radix-ui/react-label";
import "@radix-ui/react-switch";
import "@radix-ui/react-dialog";
import "@radix-ui/react-alert-dialog";
const empty = {
  message: "",
  link: "",
  priority: 0,
  active: true
};
function BreakingPage() {
  const qc = useQueryClient();
  const [editing, setEditing] = useState(null);
  const list = useQuery({
    queryKey: ["admin", "breaking"],
    queryFn: async () => {
      const {
        data,
        error
      } = await supabase.from("breaking_news").select("*").order("priority", {
        ascending: false
      }).order("created_at", {
        ascending: false
      });
      if (error) throw error;
      return data;
    }
  });
  const save = useMutation({
    mutationFn: async () => {
      if (!editing?.message?.trim()) throw new Error("Message is required");
      const payload = {
        message: editing.message.trim(),
        link: editing.link?.trim() || null,
        priority: Number(editing.priority) || 0,
        active: !!editing.active
      };
      if (editing.id) {
        const {
          error
        } = await supabase.from("breaking_news").update(payload).eq("id", editing.id);
        if (error) throw error;
      } else {
        const {
          error
        } = await supabase.from("breaking_news").insert(payload);
        if (error) throw error;
      }
    },
    onSuccess: () => {
      toast.success(editing?.id ? "Alert updated" : "Alert added");
      setEditing(null);
      qc.invalidateQueries({
        queryKey: ["admin", "breaking"]
      });
    },
    onError: (e) => toast.error(e.message)
  });
  const toggle = useMutation({
    mutationFn: async ({
      id,
      active
    }) => {
      const {
        error
      } = await supabase.from("breaking_news").update({
        active
      }).eq("id", id);
      if (error) throw error;
    },
    onSuccess: () => qc.invalidateQueries({
      queryKey: ["admin", "breaking"]
    })
  });
  const del = useMutation({
    mutationFn: async (id) => {
      const {
        error
      } = await supabase.from("breaking_news").delete().eq("id", id);
      if (error) throw error;
    },
    onSuccess: () => {
      toast.success("Deleted");
      qc.invalidateQueries({
        queryKey: ["admin", "breaking"]
      });
    },
    onError: (e) => toast.error(e.message)
  });
  return /* @__PURE__ */ jsxs(AdminShell, { title: "Breaking News Ticker", children: [
    /* @__PURE__ */ jsxs("div", { className: "flex justify-between items-center mb-4", children: [
      /* @__PURE__ */ jsxs("p", { className: "text-sm text-muted-foreground", children: [
        list.data?.length ?? 0,
        " alerts"
      ] }),
      /* @__PURE__ */ jsxs(Button, { onClick: () => setEditing({
        ...empty
      }), children: [
        /* @__PURE__ */ jsx(Plus, { className: "w-4 h-4 mr-1" }),
        " New Alert"
      ] })
    ] }),
    /* @__PURE__ */ jsxs("ul", { className: "space-y-2", children: [
      list.data?.map((b) => /* @__PURE__ */ jsxs("li", { className: "flex items-center gap-3 bg-surface border border-border p-3 rounded-sm", children: [
        /* @__PURE__ */ jsx(Switch, { checked: b.active, onCheckedChange: (c) => toggle.mutate({
          id: b.id,
          active: c
        }) }),
        /* @__PURE__ */ jsxs("div", { className: "flex-1 min-w-0", children: [
          /* @__PURE__ */ jsx("div", { className: "font-bold truncate", children: b.message }),
          /* @__PURE__ */ jsxs("div", { className: "text-xs text-muted-foreground", children: [
            "Priority ",
            b.priority,
            b.link && /* @__PURE__ */ jsxs(Fragment, { children: [
              " · ",
              /* @__PURE__ */ jsx("span", { className: "text-brand-accent truncate", children: b.link })
            ] })
          ] })
        ] }),
        /* @__PURE__ */ jsx(Button, { variant: "ghost", size: "sm", onClick: () => setEditing(b), children: /* @__PURE__ */ jsx(Edit, { className: "w-4 h-4" }) }),
        /* @__PURE__ */ jsx(DeleteButton, { onDelete: () => del.mutate(b.id), title: "Delete breaking alert?", description: `Remove "${b.message.slice(0, 60)}${b.message.length > 60 ? "…" : ""}" from the ticker.` })
      ] }, b.id)),
      !list.data?.length && /* @__PURE__ */ jsx("p", { className: "text-sm text-muted-foreground", children: "No breaking news yet." })
    ] }),
    /* @__PURE__ */ jsx(Dialog, { open: !!editing, onOpenChange: (o) => !o && setEditing(null), children: /* @__PURE__ */ jsxs(DialogContent, { children: [
      /* @__PURE__ */ jsx(DialogHeader, { children: /* @__PURE__ */ jsx(DialogTitle, { children: editing?.id ? "Edit alert" : "New alert" }) }),
      editing && /* @__PURE__ */ jsxs("div", { className: "space-y-3", children: [
        /* @__PURE__ */ jsxs("div", { children: [
          /* @__PURE__ */ jsx(Label, { children: "Message" }),
          /* @__PURE__ */ jsx(Input, { placeholder: "LILONGWE: ...", value: editing.message ?? "", onChange: (e) => setEditing({
            ...editing,
            message: e.target.value
          }) })
        ] }),
        /* @__PURE__ */ jsxs("div", { children: [
          /* @__PURE__ */ jsx(Label, { children: "Link (optional)" }),
          /* @__PURE__ */ jsx(Input, { placeholder: "https://...", value: editing.link ?? "", onChange: (e) => setEditing({
            ...editing,
            link: e.target.value
          }) })
        ] }),
        /* @__PURE__ */ jsxs("div", { children: [
          /* @__PURE__ */ jsx(Label, { children: "Priority (higher shows first)" }),
          /* @__PURE__ */ jsx(Input, { type: "number", value: editing.priority ?? 0, onChange: (e) => setEditing({
            ...editing,
            priority: Number(e.target.value)
          }) })
        ] }),
        /* @__PURE__ */ jsxs("label", { className: "flex items-center gap-2 text-sm", children: [
          /* @__PURE__ */ jsx(Switch, { checked: !!editing.active, onCheckedChange: (c) => setEditing({
            ...editing,
            active: c
          }) }),
          "Active on site"
        ] })
      ] }),
      /* @__PURE__ */ jsxs(DialogFooter, { children: [
        /* @__PURE__ */ jsx(Button, { variant: "outline", onClick: () => setEditing(null), children: "Cancel" }),
        /* @__PURE__ */ jsx(Button, { onClick: () => save.mutate(), disabled: save.isPending || !editing?.message?.trim(), children: "Save" })
      ] })
    ] }) })
  ] });
}
export {
  BreakingPage as component
};
