import { PositionsPageClient } from "@/components/positions/PositionsPageClient";
import { daysBetweenIso, toIsoDate } from "@/lib/format";
import { loadOpenPositions } from "@/lib/positions";
import { createClient } from "@/lib/supabase/server";

export default async function PositionsPage() {
  const supabase = await createClient();

  // Teams are only needed for the "Position ausschreiben" dialog's team
  // select. The division/department/team headcount overview this page used
  // to render was dropped, and with it the two employee-wide aggregation
  // queries that fed it.
  const [openPositions, { data: teams }] = await Promise.all([
    loadOpenPositions(supabase),
    supabase.from("teams").select("*").order("name"),
  ]);

  const openPositionsWithDays = openPositions.map((p) => ({ ...p, daysOpen: daysBetweenIso(toIsoDate(p.created_at)) }));

  return <PositionsPageClient openPositions={openPositionsWithDays} teams={teams ?? []} />;
}
