import {
  buildSelfServiceTimelineInput,
  buildTimelineEntriesDetailInput,
} from "./timeline-read-shared.js";
import { loadTimelineHolidayOverlays } from "./timeline-holiday-load-support.js";
import {
  buildTimelineHolidayOverlayDetailResponse,
  formatTimelineHolidayOverlays,
} from "./timeline-holiday-support.js";

type TimelineHolidayProcedureDb = Parameters<typeof loadTimelineHolidayOverlays>[0];
type TimelineHolidaySelfServiceContext = Parameters<typeof buildSelfServiceTimelineInput>[0];
type TimelineHolidaySelfServiceInput = Parameters<typeof buildSelfServiceTimelineInput>[1];
type TimelineHolidayDetailInput = Parameters<typeof buildTimelineEntriesDetailInput>[0];

export async function readMyTimelineHolidayOverlays(
  ctx: TimelineHolidaySelfServiceContext,
  input: TimelineHolidaySelfServiceInput,
) {
  const selfServiceInput = await buildSelfServiceTimelineInput(ctx, input);
  if (!selfServiceInput) {
    return [];
  }

  return loadTimelineHolidayOverlays(ctx.db, selfServiceInput);
}

export async function readTimelineHolidayOverlayDetail(
  db: TimelineHolidayProcedureDb,
  input: TimelineHolidayDetailInput,
) {
  const { period, filters, timelineInput } = buildTimelineEntriesDetailInput(input);
  const holidayOverlays = await loadTimelineHolidayOverlays(db, timelineInput);

  return buildTimelineHolidayOverlayDetailResponse({
    startDate: period.startDate,
    endDate: period.endDate,
    filters,
    overlays: formatTimelineHolidayOverlays(holidayOverlays),
  });
}
