/** Reserve roster & replacement lifecycle (UI: "Reserve trainee", never "backup"). */

export const TRAINEE_ROSTER_ROLES = ["active", "reserve", "replaced_withdrawn", "reserve_withdrawn"] as const;
export type TraineeRosterRole = (typeof TRAINEE_ROSTER_ROLES)[number];

export const TRACKED_ROSTER_ROLES: TraineeRosterRole[] = ["active", "reserve"];
export const WITHDRAWN_ROSTER_ROLES: TraineeRosterRole[] = ["replaced_withdrawn", "reserve_withdrawn"];

export const TRAINEE_REPLACEMENT_WINDOW_DAYS_KEY = "trainee.replacement_window_days";

export function parseRosterRole(raw: string | null | undefined): TraineeRosterRole {
  const v = String(raw ?? "active").trim().toLowerCase();
  if (v === "reserve" || v === "active" || v === "replaced_withdrawn" || v === "reserve_withdrawn") return v;
  if (v === "backup" || v === "true" || v === "1") return "reserve";
  return "active";
}

export function rosterRoleIsTrackedMember(role: string | null | undefined): boolean {
  const r = parseRosterRole(role);
  return r === "active" || r === "reserve";
}

export function rosterRoleIsWithdrawn(role: string | null | undefined): boolean {
  const r = parseRosterRole(role);
  return r === "replaced_withdrawn" || r === "reserve_withdrawn";
}

/** @deprecated Use rosterRoleIsTrackedMember — active and reserve are both tracked for KPI/outcomes. */
export function countsAsActiveCohortMember(role: string | null | undefined): boolean {
  return rosterRoleIsTrackedMember(role);
}

/** Prisma `in` filter for tracked roster rows. */
export function trackedRosterRoleFilter(): { in: string[] } {
  return { in: [...TRACKED_ROSTER_ROLES] };
}

/** Included in attendance KPI rate (present ÷ possible). All tracked members from batch start. */
export function rosterRoleCountsAttendanceSlots(role: string): boolean {
  return rosterRoleIsTrackedMember(role);
}

/** Included in default enrolled/registered indicator counts and dropout projection. */
export function rosterRoleCountsEnrolledKpi(role: string): boolean {
  return rosterRoleIsTrackedMember(role);
}

/** Included in dropout projection rows. */
export function rosterRoleCountsDropout(role: string): boolean {
  return rosterRoleIsTrackedMember(role);
}

/** Shown on daily attendance roster / CSV / outcome coverage expected slots. */
export function rosterRoleReceivesAttendanceCapture(role: string): boolean {
  return rosterRoleIsTrackedMember(role);
}

export function dateOnlyLocal(d: Date): Date {
  return new Date(d.getFullYear(), d.getMonth(), d.getDate());
}

/** Last calendar day inclusive for replacements: anchor + windowDays (e.g. start + 14 => two-week window including start day as day 0). */
export function replacementDeadlineInclusive(anchorStart: Date, windowDays: number): Date {
  const n = Math.max(0, Math.floor(windowDays));
  const s = dateOnlyLocal(anchorStart);
  const out = new Date(s);
  out.setDate(out.getDate() + n);
  return out;
}

export function isWithinReplacementWindow(anchorStart: Date, windowDays: number, now: Date): boolean {
  const end = replacementDeadlineInclusive(anchorStart, windowDays);
  const t = dateOnlyLocal(now);
  return t.getTime() <= end.getTime();
}
