import { redirect } from "next/navigation";
import {
  getWaterDisplaySummary,
  getWaterHeadgatesAdmin,
  getWaterIrrigators,
  getWaterAlertLog,
} from "@/actions/water-log/admin";
import { getAdminSession, TUXEDO_BRAND_ID } from "@/lib/water-admin-pin-auth";
import WaterAdminClient from "@/components/water/WaterAdminClient";

export const dynamic = "force-dynamic";

export default async function WaterAdminPage() {
  const session = await getAdminSession();
  if (!session) {
    redirect("/water/admin/login");
  }

  // Bootstrap the 4 datasets in parallel on the server so the first paint
  // already has real data — no "Loading…" skeleton flash. The client's
  // 30s polling keeps the display-summary card fresh from here.
  const [summary, headgates, users, alertLog] = await Promise.all([
    getWaterDisplaySummary(TUXEDO_BRAND_ID),
    getWaterHeadgatesAdmin(TUXEDO_BRAND_ID),
    getWaterIrrigators(TUXEDO_BRAND_ID),
    getWaterAlertLog(TUXEDO_BRAND_ID, 50),
  ]);

  return (
    <WaterAdminClient
      initialSummary={summary}
      initialHeadgates={headgates}
      initialUsers={users}
      initialAlertLog={alertLog}
    />
  );
}