import { getSessionUser } from "@/lib/session";
import { getTranslations } from "next-intl/server";
import { redirect } from "next/navigation";
import { listTenants } from "@/lib/k8s";
import { countPendingSkillActivationRequests } from "@/lib/db";
import { AdminPanel } from "@/components/admin/admin-panel";

export default async function AdminPage() {
  const user = await getSessionUser();
  if (!user) redirect("/login");

  const t = await getTranslations("admin");

  if (!user.isPlatform) {
    return (
      <div className="flex items-center justify-center min-h-[40vh]">
        <p className="text-error text-sm">{t("noAccess")}</p>
      </div>
    );
  }

  const tenants = await listTenants();
  // Phase 2.5: badge counter for the skill-activation admin queue.
  // Cheap COUNT(*) on a partial-indexed status='pending' column —
  // bounded by request volume and never expected to be high.
  const pendingSkillCount = await countPendingSkillActivationRequests().catch(
    () => 0
  );

  return (
    <div>
      <div className="mb-8 animate-in flex items-end justify-between gap-4 flex-wrap">
        <div>
          <h1 className="font-display text-2xl font-semibold accent-rule mb-2">
            {t("title")}
          </h1>
          <p className="text-text-secondary text-sm mt-4">{t("subtitle")}</p>
        </div>
        {/* Sub-tools: links to other admin pages. Plain links rather
            than nav-shell entries — these are platform-team utilities,
            not main navigation. */}
        <div className="flex items-center gap-2">
          <a
            href="/admin/skills/pending"
            className={`text-sm px-4 py-2 rounded-lg border transition-colors flex items-center gap-2 ${
              pendingSkillCount > 0
                ? "border-warning text-warning hover:bg-warning/10"
                : "border-border text-text-secondary hover:text-text-primary hover:border-text-secondary"
            }`}
          >
            <span>{t("skillsQueueTool")}</span>
            {pendingSkillCount > 0 && (
              <span className="text-xs px-1.5 py-0.5 rounded bg-warning text-surface-0 font-semibold">
                {pendingSkillCount}
              </span>
            )}
          </a>
          <a
            href="/admin/billing"
            className="text-sm px-4 py-2 rounded-lg border border-border text-text-secondary hover:text-text-primary hover:border-text-secondary transition-colors"
          >
            {t("billingTool")}
          </a>
          <a
            href="/admin/cron"
            className="text-sm px-4 py-2 rounded-lg border border-border text-text-secondary hover:text-text-primary hover:border-text-secondary transition-colors"
          >
            {t("cronTool")}
          </a>
          <a
            href="/admin/openclaw"
            className="text-sm px-4 py-2 rounded-lg border border-border text-text-secondary hover:text-text-primary hover:border-text-secondary transition-colors"
          >
            {t("openclawTool")}
          </a>
        </div>
      </div>

      <div className="animate-in animate-in-delay-1">
        <AdminPanel initialTenants={tenants} />
      </div>
    </div>
  );
}
