import { e as createComponent, l as renderHead, k as renderComponent, r as renderTemplate } from '../../../chunks/astro/server_DePCulMI.mjs';
import 'piccolore';
/* empty css                                        */
import { jsx, jsxs } from 'react/jsx-runtime';
import { useState, useEffect } from 'react';
import { A as AuthProvider, a as AuthGuard, u as useAuth } from '../../../chunks/AdminAuth_CWvmd3cU.mjs';
import { A as AdminLayout } from '../../../chunks/AdminLayout_CZSIXmew.mjs';
import { ResponsiveContainer, BarChart, XAxis, YAxis, Tooltip, Bar } from 'recharts';
export { renderers } from '../../../renderers.mjs';

function FeedHealthContent() {
  const { token } = useAuth();
  const [feeds, setFeeds] = useState([]);
  const [selectedFeed, setSelectedFeed] = useState(null);
  const [history, setHistory] = useState([]);
  const [loading, setLoading] = useState(true);
  const headers = { Authorization: `Bearer ${token}` };
  useEffect(() => {
    fetch("/api/admin/feeds/health", { headers }).then((r) => r.json()).then((res) => setFeeds(res.feeds || [])).finally(() => setLoading(false));
  }, []);
  useEffect(() => {
    if (!selectedFeed) return;
    fetch(`/api/admin/feeds/health?feed_id=${selectedFeed}`, { headers }).then((r) => r.json()).then((res) => setHistory(res.history || []));
  }, [selectedFeed]);
  const formatDuration = (d) => {
    if (!d) return "-";
    const m = d.minutes || 0;
    const s = d.seconds || 0;
    return m > 0 ? `${m}m ${s}s` : `${s}s`;
  };
  const timeAgo = (dt) => {
    if (!dt) return "Nooit";
    const diff = Date.now() - new Date(dt).getTime();
    const h = Math.floor(diff / 36e5);
    if (h < 1) return `${Math.floor(diff / 6e4)}m geleden`;
    if (h < 24) return `${h}u geleden`;
    return `${Math.floor(h / 24)}d geleden`;
  };
  if (loading) return /* @__PURE__ */ jsx(AdminLayout, { title: "Feed Health", children: /* @__PURE__ */ jsx("div", { className: "flex items-center justify-center py-20", children: /* @__PURE__ */ jsx("div", { className: "w-8 h-8 border-2 border-[#00FF6A] border-t-transparent rounded-full animate-spin" }) }) });
  return /* @__PURE__ */ jsx(AdminLayout, { title: "Feed Health", children: /* @__PURE__ */ jsxs("div", { className: "space-y-4", children: [
    feeds.map((f) => /* @__PURE__ */ jsxs(
      "div",
      {
        className: `bg-[#141414] border rounded-xl p-5 cursor-pointer transition-colors
            ${f.stale ? "border-[#FF3B3B]/50" : f.high_errors ? "border-[#FFD700]/50" : "border-[#333]"}
            ${selectedFeed === f.id ? "ring-1 ring-[#00FF6A]" : ""}`,
        onClick: () => setSelectedFeed(selectedFeed === f.id ? null : f.id),
        children: [
          /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-between", children: [
            /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-3", children: [
              /* @__PURE__ */ jsx("span", { className: `w-2 h-2 rounded-full ${f.is_enabled ? f.stale ? "bg-[#FF3B3B]" : "bg-[#00FF6A]" : "bg-[#666]"}` }),
              /* @__PURE__ */ jsx("span", { className: "text-[#F5F5F5] font-medium", children: f.name }),
              f.stale && /* @__PURE__ */ jsx("span", { className: "text-xs bg-[#FF3B3B]/20 text-[#FF3B3B] px-2 py-0.5 rounded", children: "Stale (>24h)" }),
              f.high_errors && /* @__PURE__ */ jsx("span", { className: "text-xs bg-[#FFD700]/20 text-[#FFD700] px-2 py-0.5 rounded", children: "High Errors" })
            ] }),
            /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-6 text-sm text-[#999]", children: [
              /* @__PURE__ */ jsxs("span", { title: "Products", children: [
                f.product_count || 0,
                " prod."
              ] }),
              /* @__PURE__ */ jsxs("span", { title: "Sneaker ratio", children: [
                f.sneaker_ratio,
                "% sneakers"
              ] }),
              /* @__PURE__ */ jsx("span", { title: "Last sync", children: timeAgo(f.last_sync_at) }),
              /* @__PURE__ */ jsx("span", { title: "Duration", children: formatDuration(f.duration) }),
              /* @__PURE__ */ jsx("span", { className: f.last_sync_status === "success" ? "text-[#00FF6A]" : "text-[#FF3B3B]", children: f.last_sync_status || "-" })
            ] })
          ] }),
          selectedFeed === f.id && /* @__PURE__ */ jsxs("div", { className: "mt-4 pt-4 border-t border-[#222]", children: [
            /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-5 gap-4 text-sm mb-4", children: [
              /* @__PURE__ */ jsxs("div", { children: [
                /* @__PURE__ */ jsx("span", { className: "text-[#666]", children: "Fetched:" }),
                " ",
                /* @__PURE__ */ jsx("span", { className: "text-[#F5F5F5]", children: f.products_fetched ?? "-" })
              ] }),
              /* @__PURE__ */ jsxs("div", { children: [
                /* @__PURE__ */ jsx("span", { className: "text-[#666]", children: "New:" }),
                " ",
                /* @__PURE__ */ jsx("span", { className: "text-[#00FF6A]", children: f.products_new ?? "-" })
              ] }),
              /* @__PURE__ */ jsxs("div", { children: [
                /* @__PURE__ */ jsx("span", { className: "text-[#666]", children: "Updated:" }),
                " ",
                /* @__PURE__ */ jsx("span", { className: "text-[#F5F5F5]", children: f.products_updated ?? "-" })
              ] }),
              /* @__PURE__ */ jsxs("div", { children: [
                /* @__PURE__ */ jsx("span", { className: "text-[#666]", children: "Removed:" }),
                " ",
                /* @__PURE__ */ jsx("span", { className: "text-[#FF3B3B]", children: f.products_removed ?? "-" })
              ] }),
              /* @__PURE__ */ jsxs("div", { children: [
                /* @__PURE__ */ jsx("span", { className: "text-[#666]", children: "Errors:" }),
                " ",
                /* @__PURE__ */ jsx("span", { className: f.errors_count > 0 ? "text-[#FFD700]" : "text-[#F5F5F5]", children: f.errors_count ?? "-" })
              ] })
            ] }),
            f.last_error && /* @__PURE__ */ jsx("div", { className: "text-xs text-[#FF3B3B] bg-[#FF3B3B]/10 p-2 rounded mb-4 max-h-20 overflow-auto", children: f.last_error }),
            history.length > 0 && /* @__PURE__ */ jsxs("div", { children: [
              /* @__PURE__ */ jsx("h4", { className: "text-sm text-[#999] mb-2", children: "Sync History" }),
              /* @__PURE__ */ jsx(ResponsiveContainer, { width: "100%", height: 150, children: /* @__PURE__ */ jsxs(BarChart, { data: [...history].reverse(), children: [
                /* @__PURE__ */ jsx(XAxis, { dataKey: "started_at", tick: false }),
                /* @__PURE__ */ jsx(YAxis, { tick: { fill: "#999", fontSize: 11 } }),
                /* @__PURE__ */ jsx(Tooltip, { contentStyle: { background: "#1A1A1A", border: "1px solid #333", borderRadius: 8, fontSize: 12 } }),
                /* @__PURE__ */ jsx(Bar, { dataKey: "products_new", stackId: "a", fill: "#00FF6A", name: "New" }),
                /* @__PURE__ */ jsx(Bar, { dataKey: "products_updated", stackId: "a", fill: "#FFD700", name: "Updated" }),
                /* @__PURE__ */ jsx(Bar, { dataKey: "products_removed", stackId: "a", fill: "#FF3B3B", name: "Removed" })
              ] }) })
            ] })
          ] })
        ]
      },
      f.id
    )),
    feeds.length === 0 && /* @__PURE__ */ jsx("div", { className: "text-[#666] text-center py-20", children: "Geen feeds geconfigureerd" })
  ] }) });
}
function FeedHealth() {
  return /* @__PURE__ */ jsx(AuthProvider, { children: /* @__PURE__ */ jsx(AuthGuard, { children: /* @__PURE__ */ jsx(FeedHealthContent, {}) }) });
}

const $$Health = createComponent(($$result, $$props, $$slots) => {
  return renderTemplate`<html lang="nl" class="dark"> <head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Feed Health — SneakerPicks Admin</title>${renderHead()}</head> <body class="min-h-screen bg-[#0A0A0A] text-[#F5F5F5]"> ${renderComponent($$result, "FeedHealth", FeedHealth, { "client:load": true, "client:component-hydration": "load", "client:component-path": "C:/Users/Roel/clawd/sneakerpicks/src/components/admin/FeedHealth", "client:component-export": "default" })} </body></html>`;
}, "C:/Users/Roel/clawd/sneakerpicks/src/pages/admin/feeds/health.astro", void 0);

const $$file = "C:/Users/Roel/clawd/sneakerpicks/src/pages/admin/feeds/health.astro";
const $$url = "/admin/feeds/health";

const _page = /*#__PURE__*/Object.freeze(/*#__PURE__*/Object.defineProperty({
  __proto__: null,
  default: $$Health,
  file: $$file,
  url: $$url
}, Symbol.toStringTag, { value: 'Module' }));

const page = () => _page;

export { page };
