import { Prisma } from "@capakraken/db";

export function buildTimelineAllocationMetadata(input: {
  existingMetadata: Record<string, unknown> | null | undefined;
  includeSaturday: boolean | undefined;
}): { metadata: Record<string, unknown>; includeSaturday: boolean } {
  const existingMetadata = input.existingMetadata ?? {};
  const metadata: Record<string, unknown> = {
    ...existingMetadata,
    ...(input.includeSaturday !== undefined
      ? { includeSaturday: input.includeSaturday }
      : {}),
  };
  const includeSaturday =
    input.includeSaturday ?? (existingMetadata.includeSaturday as boolean | undefined) ?? false;

  return { metadata, includeSaturday };
}

export function buildTimelineAllocationUpdateAuditChanges(input: {
  allocationId: string;
  previousHoursPerDay: number;
  previousStartDate: Date;
  previousEndDate: Date;
  nextAllocationId: string;
  nextHoursPerDay: number;
  nextStartDate: Date;
  nextEndDate: Date;
  includeSaturday: boolean;
}): Prisma.InputJsonValue {
  return {
    before: {
      id: input.allocationId,
      hoursPerDay: input.previousHoursPerDay,
      startDate: input.previousStartDate,
      endDate: input.previousEndDate,
    },
    after: {
      id: input.nextAllocationId,
      hoursPerDay: input.nextHoursPerDay,
      startDate: input.nextStartDate,
      endDate: input.nextEndDate,
      includeSaturday: input.includeSaturday,
    },
  } as unknown as Prisma.InputJsonValue;
}

export function buildTimelineAllocationEntryUpdate(input: {
  hoursPerDay: number;
  startDate: Date;
  endDate: Date;
  metadata: Record<string, unknown>;
  dailyCostCents: number;
  role?: string | undefined;
}) {
  return {
    demandRequirementUpdate: {
      hoursPerDay: input.hoursPerDay,
      startDate: input.startDate,
      endDate: input.endDate,
      metadata: input.metadata,
      ...(input.role !== undefined ? { role: input.role } : {}),
    },
    assignmentUpdate: {
      hoursPerDay: input.hoursPerDay,
      startDate: input.startDate,
      endDate: input.endDate,
      dailyCostCents: input.dailyCostCents,
      metadata: input.metadata,
      ...(input.role !== undefined ? { role: input.role } : {}),
    },
  };
}
