import { AllocationStatus } from "@capakraken/shared";

export function calculateTimelineAllocationPercentage(hoursPerDay: number): number {
  return Math.min(100, Math.round((hoursPerDay / 8) * 100));
}

export function buildTimelineQuickAssignMetadata(source: "quickAssign" | "batchQuickAssign") {
  return { source } satisfies Record<string, unknown>;
}

export function buildTimelineQuickAssignAssignmentInput(input: {
  resourceId: string;
  projectId: string;
  startDate: Date;
  endDate: Date;
  hoursPerDay: number;
  role: string;
  roleId?: string | undefined;
  status: AllocationStatus;
  source: "quickAssign" | "batchQuickAssign";
}) {
  return {
    resourceId: input.resourceId,
    projectId: input.projectId,
    startDate: input.startDate,
    endDate: input.endDate,
    hoursPerDay: input.hoursPerDay,
    percentage: calculateTimelineAllocationPercentage(input.hoursPerDay),
    role: input.role,
    ...(input.roleId !== undefined ? { roleId: input.roleId } : {}),
    status: input.status,
    metadata: buildTimelineQuickAssignMetadata(input.source),
  };
}
