import type { Prisma } from "@capakraken/db";
import { findUniqueOrThrow } from "../db/helpers.js";
import type { ShiftDbClient } from "./timeline-read-shared.js";

export const projectShiftContextSelect = {
  id: true,
  budgetCents: true,
  winProbability: true,
  startDate: true,
  endDate: true,
} as const satisfies Prisma.ProjectSelect;

export const timelineProjectContextSelect = {
  id: true,
  name: true,
  shortCode: true,
  orderType: true,
  budgetCents: true,
  winProbability: true,
  status: true,
  startDate: true,
  endDate: true,
  staffingReqs: true,
} as const satisfies Prisma.ProjectSelect;

export const timelineShiftPreviewProjectSelect = {
  id: true,
  name: true,
  shortCode: true,
  status: true,
  responsiblePerson: true,
  startDate: true,
  endDate: true,
} as const satisfies Prisma.ProjectSelect;

export const timelineBudgetStatusProjectSelect = {
  id: true,
  name: true,
  shortCode: true,
  budgetCents: true,
  winProbability: true,
  startDate: true,
  endDate: true,
} as const satisfies Prisma.ProjectSelect;

export async function findTimelineProjectOrThrow<TSelect extends Prisma.ProjectSelect>(
  db: Pick<ShiftDbClient, "project">,
  input: {
    projectId: string;
    select: TSelect;
  },
): Promise<Prisma.ProjectGetPayload<{ select: TSelect }>> {
  return findUniqueOrThrow(
    db.project.findUnique({
      where: { id: input.projectId },
      select: input.select,
    }),
    "Project",
  ) as Promise<Prisma.ProjectGetPayload<{ select: TSelect }>>;
}
