import { jsxs, jsx } from "react/jsx-runtime";
import { Link } from "@tanstack/react-router";
import { useQuery } from "@tanstack/react-query";
import { s as supabase } from "./client-CJCZGN4z.js";
import { A as AdminShell } from "./AdminShell-fo83oaxY.js";
import { C as Card, a as CardHeader, b as CardTitle, c as CardContent } from "./card-DU714E9E.js";
import { FileText, Inbox, Radio, MessageSquare, Megaphone, Eye } from "lucide-react";
import "@supabase/supabase-js";
import "./use-auth-DoD1gvcu.js";
import "react";
import "./button-BXrfXN_b.js";
import "@radix-ui/react-slot";
import "class-variance-authority";
import "clsx";
import "tailwind-merge";
function StatCard({
  label,
  value,
  icon: Icon,
  to
}) {
  return /* @__PURE__ */ jsx(Link, { to, className: "block", children: /* @__PURE__ */ jsxs(Card, { className: "hover:border-brand-accent transition-colors", children: [
    /* @__PURE__ */ jsxs(CardHeader, { className: "flex flex-row items-center justify-between pb-2", children: [
      /* @__PURE__ */ jsx(CardTitle, { className: "text-xs uppercase tracking-widest text-muted-foreground", children: label }),
      /* @__PURE__ */ jsx(Icon, { className: "w-4 h-4 text-brand-accent" })
    ] }),
    /* @__PURE__ */ jsx(CardContent, { children: /* @__PURE__ */ jsx("div", { className: "text-3xl font-serif font-black", children: value }) })
  ] }) });
}
function Dashboard() {
  const stats = useQuery({
    queryKey: ["admin", "dashboard-stats"],
    queryFn: async () => {
      const [pub, draft, sub, breaking, ads, polls, views] = await Promise.all([supabase.from("articles").select("id", {
        count: "exact",
        head: true
      }).eq("status", "published"), supabase.from("articles").select("id", {
        count: "exact",
        head: true
      }).eq("status", "draft"), supabase.from("citizen_submissions").select("id", {
        count: "exact",
        head: true
      }).eq("status", "pending"), supabase.from("breaking_news").select("id", {
        count: "exact",
        head: true
      }).eq("active", true), supabase.from("ad_slots").select("id", {
        count: "exact",
        head: true
      }).eq("active", true), supabase.from("polls").select("id", {
        count: "exact",
        head: true
      }).eq("active", true), supabase.from("article_views").select("id", {
        count: "exact",
        head: true
      })]);
      return {
        published: pub.count ?? 0,
        drafts: draft.count ?? 0,
        submissions: sub.count ?? 0,
        breaking: breaking.count ?? 0,
        ads: ads.count ?? 0,
        polls: polls.count ?? 0,
        views: views.count ?? 0
      };
    }
  });
  const recent = useQuery({
    queryKey: ["admin", "recent-articles"],
    queryFn: async () => {
      const {
        data
      } = await supabase.from("articles").select("id, title, status, updated_at, author_name").order("updated_at", {
        ascending: false
      }).limit(8);
      return data ?? [];
    }
  });
  const s = stats.data;
  return /* @__PURE__ */ jsxs(AdminShell, { title: "Newsroom Dashboard", children: [
    /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 md:grid-cols-3 lg:grid-cols-6 gap-4 mb-8", children: [
      /* @__PURE__ */ jsx(StatCard, { label: "Published", value: s?.published ?? "—", icon: FileText, to: "/admin/articles" }),
      /* @__PURE__ */ jsx(StatCard, { label: "Drafts", value: s?.drafts ?? "—", icon: FileText, to: "/admin/articles" }),
      /* @__PURE__ */ jsx(StatCard, { label: "Pending Submissions", value: s?.submissions ?? "—", icon: Inbox, to: "/admin/submissions" }),
      /* @__PURE__ */ jsx(StatCard, { label: "Breaking", value: s?.breaking ?? "—", icon: Radio, to: "/admin/breaking" }),
      /* @__PURE__ */ jsx(StatCard, { label: "Active Polls", value: s?.polls ?? "—", icon: MessageSquare, to: "/admin/polls" }),
      /* @__PURE__ */ jsx(StatCard, { label: "Active Ads", value: s?.ads ?? "—", icon: Megaphone, to: "/admin/ads" })
    ] }),
    /* @__PURE__ */ jsxs(Card, { children: [
      /* @__PURE__ */ jsxs(CardHeader, { className: "flex flex-row items-center justify-between", children: [
        /* @__PURE__ */ jsx(CardTitle, { className: "font-serif", children: "Recent activity" }),
        /* @__PURE__ */ jsx(Eye, { className: "w-4 h-4 text-muted-foreground" })
      ] }),
      /* @__PURE__ */ jsx(CardContent, { children: !recent.data?.length ? /* @__PURE__ */ jsxs("p", { className: "text-sm text-muted-foreground", children: [
        "No articles yet. ",
        /* @__PURE__ */ jsx(Link, { to: "/admin/articles", className: "text-brand-accent underline", children: "Create your first story" }),
        "."
      ] }) : /* @__PURE__ */ jsx("ul", { className: "divide-y divide-border", children: recent.data.map((a) => /* @__PURE__ */ jsxs("li", { className: "py-3 flex items-center justify-between gap-4", children: [
        /* @__PURE__ */ jsxs("div", { className: "min-w-0", children: [
          /* @__PURE__ */ jsx("div", { className: "font-bold truncate", children: a.title }),
          /* @__PURE__ */ jsxs("div", { className: "text-xs text-muted-foreground", children: [
            a.author_name ?? "Staff",
            " · ",
            new Date(a.updated_at).toLocaleString()
          ] })
        ] }),
        /* @__PURE__ */ jsx("span", { className: `text-[10px] font-black uppercase px-2 py-0.5 rounded-sm ${a.status === "published" ? "bg-brand-accent/10 text-brand-accent" : "bg-muted text-muted-foreground"}`, children: a.status })
      ] }, a.id)) }) })
    ] }),
    /* @__PURE__ */ jsxs("p", { className: "text-xs text-muted-foreground mt-6", children: [
      "Total recorded views across all articles: ",
      /* @__PURE__ */ jsx("strong", { children: s?.views ?? 0 })
    ] })
  ] });
}
export {
  Dashboard as component
};
