import { prisma } from "@/lib/prisma";

// Cross-cutting departments (e.g. HQ) serve every department and company, so
// their KPIs & staff must appear under every department/company filter.

/** IDs of departments flagged spansAll (e.g. HQ). */
export async function spanAllDepartmentIds(): Promise<string[]> {
  const rows = await prisma.department.findMany({
    where: { spansAll: true },
    select: { id: true },
  });
  return rows.map((r) => r.id);
}

/**
 * Resolve a `departmentId` filter value into a Prisma `departmentId` condition.
 * Selecting a normal department also pulls in cross-cutting (HQ) items; selecting
 * a cross-cutting department itself matches only that department.
 */
export function departmentIdFilter(departmentId: string, spanIds: string[]) {
  if (spanIds.includes(departmentId)) return departmentId;
  return { in: [departmentId, ...spanIds] };
}
