import { SWI_DOCUMENT_STATUS } from "../constants/swiDocumentStatus.js";

export const DOCUMENT_STATUS_CHART_ITEMS = [
  {
    statusId: SWI_DOCUMENT_STATUS.OPENED,
    label: "Opened",
    tone: "opened",
  },
  {
    statusId: SWI_DOCUMENT_STATUS.ASSIGNED,
    label: "Assigned",
    tone: "assigned",
  },
  {
    statusId: SWI_DOCUMENT_STATUS.IN_PROGRESS,
    label: "In Progress",
    tone: "in-progress",
  },
  {
    statusId: SWI_DOCUMENT_STATUS.COMPLETED,
    label: "Completed",
    tone: "completed",
  },
];

export function countDocumentsByStatus(documents = []) {
  const counts = {
    [SWI_DOCUMENT_STATUS.OPENED]: 0,
    [SWI_DOCUMENT_STATUS.ASSIGNED]: 0,
    [SWI_DOCUMENT_STATUS.IN_PROGRESS]: 0,
    [SWI_DOCUMENT_STATUS.COMPLETED]: 0,
    unknown: 0,
  };

  for (const doc of documents) {
    const id = Number(doc.document_status_id);
    if (
      id === SWI_DOCUMENT_STATUS.OPENED ||
      id === SWI_DOCUMENT_STATUS.ASSIGNED ||
      id === SWI_DOCUMENT_STATUS.IN_PROGRESS ||
      id === SWI_DOCUMENT_STATUS.COMPLETED
    ) {
      counts[id] += 1;
    } else if (!id) {
      counts[SWI_DOCUMENT_STATUS.OPENED] += 1;
    } else {
      counts.unknown += 1;
    }
  }

  return counts;
}

export function documentStatusChartSeries(documents = []) {
  const counts = countDocumentsByStatus(documents);
  return DOCUMENT_STATUS_CHART_ITEMS.map((item) => ({
    ...item,
    value: counts[item.statusId] ?? 0,
  }));
}
