import type { Metadata } from "next";
import { db, adminWebhooks } from "@epicure/db";
import { AdminWebhooksManager } from "@/components/admin/admin-webhooks-manager";
import { requireFullAdminPage } from "@/lib/require-admin-page";

export const metadata: Metadata = {};

export default async function AdminWebhooksPage() {
  await requireFullAdminPage();
  const rows = await db
    .select({
      id: adminWebhooks.id,
      url: adminWebhooks.url,
      events: adminWebhooks.events,
      active: adminWebhooks.active,
      createdAt: adminWebhooks.createdAt,
    })
    .from(adminWebhooks);

  const webhooks = rows.map((w) => ({ ...w, createdAt: w.createdAt.toISOString() }));

  return (
    <div className="space-y-6">
      <div>
        <h1 className="text-2xl font-bold tracking-tight">Webhooks</h1>
        <p className="text-muted-foreground text-sm mt-1">
          Site-wide ops events (new signups, support tickets, reports) — deliver to any HTTP endpoint (Slack/Discord incoming webhook, internal alerting, etc). Fires regardless of who&apos;s involved, unlike the per-user webhooks under Settings.
        </p>
      </div>
      <AdminWebhooksManager initialWebhooks={webhooks} />
    </div>
  );
}
