import { OrgChartClient } from "@/components/orgchart/OrgChartClient";
import { loadOpenPositions } from "@/lib/positions";
import { createClient } from "@/lib/supabase/server";

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

  const [
    { data: employees },
    { data: divisions },
    { data: departments },
    { data: teams },
    openPositions,
    { data: reorgScenarios },
  ] = await Promise.all([
    supabase
      .from("employees")
      .select("id, personnel_number, first_name, last_name, job_title, manager_id, team_id, division_id, is_lead, org_level")
      .in("status", ["Aktiv", "Karenz"]),
    supabase.from("divisions").select("*").order("name"),
    supabase.from("departments").select("*"),
    supabase.from("teams").select("*"),
    loadOpenPositions(supabase),
    supabase
      .from("reorg_scenarios")
      .select("id, name, effective_date, applied, applied_at")
      .eq("applied", true)
      .order("applied_at", { ascending: false })
      .limit(5),
  ]);

  return (
    <OrgChartClient
      employees={employees ?? []}
      divisions={divisions ?? []}
      departments={departments ?? []}
      teams={teams ?? []}
      openPositions={openPositions}
      reorgScenarios={reorgScenarios ?? []}
    />
  );
}
