export const WORKSTREAM_STATUSES = [
  "proposed",
  "ready",
  "active",
  "blocked",
  "backlog",
  "finished",
  "archived",
];

export const OPEN_WORKSTREAM_STATUSES = ["ready", "active", "blocked"];
export const CLOSED_WORKSTREAM_STATUSES = ["finished", "archived"];

export const LEGACY_STATUS_ALIASES = {
  todo: "ready",
  done: "finished",
  completed: "finished",
  accepted: "finished",
};

export function normalizeWorkstreamStatus(status) {
  const value = String(status ?? "").trim().toLowerCase();
  return LEGACY_STATUS_ALIASES[value] ?? value;
}

export function isClosedWorkstream(status) {
  return CLOSED_WORKSTREAM_STATUSES.includes(normalizeWorkstreamStatus(status));
}

export function isOpenWorkstream(status) {
  return OPEN_WORKSTREAM_STATUSES.includes(normalizeWorkstreamStatus(status));
}

export function isStalledWorkstream(w) {
  const staleAt = new Date(Date.now() - 7 * 24 * 60 * 60 * 1000);
  const openTasks = (w.todo ?? 0) + (w.progress ?? 0) + (w.wait ?? 0);
  return ["active", "blocked"].includes(normalizeWorkstreamStatus(w.status))
    && new Date(w.updated_at) < staleAt
    && (w.done ?? 0) > 0
    && openTasks > 0;
}

export function needsReviewWorkstream(w) {
  return Array.isArray(w.health_labels) && w.health_labels.includes("needs_review");
}
