import type { PrismaClient } from "@capakraken/db";
import { calculateAllocation } from "@capakraken/engine";
import type {
  RecurrencePattern,
  WeekdayAvailability,
} from "@capakraken/shared";
import {
  buildTimelineAbsenceDays,
  loadTimelineCalculationRules,
} from "./timeline-cost-load-support.js";

export async function calculateTimelineAllocationDailyCost(input: {
  db: PrismaClient;
  resourceId: string;
  lcrCents: number;
  hoursPerDay: number;
  startDate: Date;
  endDate: Date;
  availability: WeekdayAvailability;
  includeSaturday?: boolean | undefined;
  recurrence?: RecurrencePattern | undefined;
}): Promise<number> {
  const [absenceData, calculationRules] = await Promise.all([
    buildTimelineAbsenceDays(input.db, input.resourceId, input.startDate, input.endDate),
    loadTimelineCalculationRules(input.db),
  ]);

  return calculateAllocation({
    lcrCents: input.lcrCents,
    hoursPerDay: input.hoursPerDay,
    startDate: input.startDate,
    endDate: input.endDate,
    availability: input.availability,
    includeSaturday: input.includeSaturday ?? false,
    ...(input.recurrence ? { recurrence: input.recurrence } : {}),
    vacationDates: absenceData.legacyVacationDates,
    absenceDays: absenceData.absenceDays,
    calculationRules,
  }).dailyCostCents;
}
