/** Infer workflow section codes from section title (no ptw_section_type table). */

function normalize(value) {
  return String(value || "").trim().toLowerCase();
}

export function inferSectionTypeCode(section) {
  const title = normalize(section?.title);
  if (
    title.includes("general") ||
    title.includes("detail") ||
    (title.includes("video") && !title.includes("risk"))
  ) {
    return "general";
  }
  if (title.includes("risk")) return "risk_assessment";
  if (title.includes("handover")) return "handover";
  if (title.includes("handback") || title.includes("hand back")) return "handback";
  if (title.includes("audit")) return "audit";
  if (title.includes("precaution") || title.includes("checklist")) {
    return "precaution_checklist";
  }
  return "standard";
}

export function withInferredSectionType(section) {
  if (!section || typeof section !== "object") return section;
  const code = inferSectionTypeCode(section);
  return {
    ...section,
    section_type_code: code,
    section_type_name: section.section_type_name ?? code
  };
}
