import { redirect } from "next/navigation";
import { getTranslations } from "next-intl/server";
import { getSessionUser } from "@/lib/session";
import {
  getLastSuccessfulCronRuns,
  listRecentCronRuns,
} from "@/lib/db";
import { CronControls } from "@/components/admin/cron/cron-controls";

/**
 * /admin/cron — automation dashboard.
 *
 * Shows:
 *   - Last successful run of each kind, with relative time
 *   - Two "Run now" buttons (admin-triggered manual sweeps)
 *   - Recent runs table (last 30)
 *
 * Platform-admin gated server-side.
 */
export default async function AdminCronPage() {
  const user = await getSessionUser();
  if (!user || !user.isPlatform) redirect("/login");
  const t = await getTranslations("adminCron");

  const [recent, lastSuccess] = await Promise.all([
    listRecentCronRuns(30),
    getLastSuccessfulCronRuns(),
  ]);

  return (
    <main className="max-w-5xl mx-auto px-6 py-8">
      <div className="mb-8 animate-in">
        <h1 className="font-display text-2xl font-semibold accent-rule">
          {t("title")}
        </h1>
        <p className="text-sm text-text-secondary mt-3">{t("subtitle")}</p>
      </div>
      <CronControls
        initialRecent={recent}
        initialLastSuccess={lastSuccess}
      />
    </main>
  );
}
