import {
  SURVEY_ALL_DOC_TYPES,
  EXPECTED_RETROFIT_ASSESSMENT_DOC_TYPES,
  COORDINATION_DOC_TYPES,
  DESIGN_DOC_TYPES,
} from "./types";
import type { PropertyDocument, MeasureDocProgress } from "./types";

export function splitDocumentsByType(docs: PropertyDocument[]): {
  docs: PropertyDocument[];
  coordinationDocs: PropertyDocument[];
  designDocs: PropertyDocument[];
  installDocs: PropertyDocument[];
} {
  return {
    docs: docs.filter((d) => SURVEY_ALL_DOC_TYPES.has(d.docType)),
    coordinationDocs: docs.filter((d) => COORDINATION_DOC_TYPES.has(d.docType)),
    designDocs: docs.filter((d) => DESIGN_DOC_TYPES.has(d.docType)),
    installDocs: docs.filter(
      (d) =>
        !SURVEY_ALL_DOC_TYPES.has(d.docType) &&
        !COORDINATION_DOC_TYPES.has(d.docType) &&
        !DESIGN_DOC_TYPES.has(d.docType),
    ),
  };
}

export function getMissingSurveyDocTypes(surveyDocs: PropertyDocument[]): string[] {
  const present = new Set(surveyDocs.map((d) => d.docType));
  return EXPECTED_RETROFIT_ASSESSMENT_DOC_TYPES.filter((t) => !present.has(t));
}

export function getUnassignedInstallDocs(
  installDocs: PropertyDocument[],
  measureProgress: MeasureDocProgress[],
): PropertyDocument[] {
  const knownMeasures = new Set(measureProgress.map((m) => m.measureName));
  return installDocs.filter((d) => !d.measureName || !knownMeasures.has(d.measureName));
}
