import type { Etape } from "@xtablo/shared-types";

const STARTED_ETAPE_STATUSES = new Set(["in_progress", "in_review", "done"]);

export interface EtapeProgressStats {
  total: number;
  started: number;
  done: number;
  startedPercentage: number;
  donePercentage: number;
}

export function getEtapeProgressStats(etapes: Etape[]): EtapeProgressStats {
  const total = etapes.length;
  const done = etapes.filter((etape) => etape.status === "done").length;
  const started = etapes.filter((etape) =>
    STARTED_ETAPE_STATUSES.has(etape.status ?? "todo")
  ).length;

  if (total === 0) {
    return {
      total: 0,
      started: 0,
      done: 0,
      startedPercentage: 0,
      donePercentage: 0,
    };
  }

  return {
    total,
    started,
    done,
    startedPercentage: Math.round((started / total) * 100),
    donePercentage: Math.round((done / total) * 100),
  };
}
