/** Section kinds for the step-by-step permit form workflow. */

import { PTW_SECTION_SKIP_ENABLED } from "../config/ptwFeatures.js";

export const SECTION_KIND = {
  GENERAL: "general",
  RISK: "risk_assessment",
  HANDOVER: "handover",
  HANDBACK: "handback",
  AUDIT: "audit",
  STANDARD: "standard"
};

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

/**
 * Resolve section UI kind from `section_type_code` (DB) or title.
 */
export function resolveSectionKind(section) {
  const code = normalize(section?.section_type_code);
  if (code === "general") return SECTION_KIND.GENERAL;
  if (code === "risk_assessment" || code === "risk") return SECTION_KIND.RISK;
  if (code === "handover") return SECTION_KIND.HANDOVER;
  if (code === "handback") return SECTION_KIND.HANDBACK;
  if (code === "audit") return SECTION_KIND.AUDIT;
  if (
    code === "precaution_checklist" ||
    code === "precaution" ||
    code === "authorization" ||
    code === "auth_acceptance" ||
    code === "standard"
  ) {
    return SECTION_KIND.STANDARD;
  }

  const title = normalize(section?.title);
  if (
    title.includes("general") ||
    title.includes("detail") ||
    (title.includes("video") && !title.includes("risk"))
  ) {
    return SECTION_KIND.GENERAL;
  }
  if (title.includes("risk")) return SECTION_KIND.RISK;
  if (title.includes("handover")) return SECTION_KIND.HANDOVER;
  if (title.includes("handback") || title.includes("hand back")) {
    return SECTION_KIND.HANDBACK;
  }
  if (title.includes("audit")) {
    return SECTION_KIND.AUDIT;
  }
  if (title.includes("precaution") || title.includes("checklist")) {
    return SECTION_KIND.STANDARD;
  }

  return SECTION_KIND.STANDARD;
}

function sectionOrderValue(section) {
  const raw = section?.section_in_form_order;
  if (raw === null || raw === undefined || raw === "") return 999999;
  const n = Number(raw);
  return Number.isFinite(n) ? n : 999999;
}

export function sortSections(sections) {
  return [...(sections || [])].sort((a, b) => {
    const oa = sectionOrderValue(a);
    const ob = sectionOrderValue(b);
    if (oa !== ob) return oa - ob;
    return Number(a.section_id) - Number(b.section_id);
  });
}

/** Handover, handback, and audit are permit-level lifecycle — not part of general/specialized form fill or preview sections on the opening form. */
export function isLifecycleSectionKind(sectionKind) {
  return (
    sectionKind === SECTION_KIND.HANDOVER ||
    sectionKind === SECTION_KIND.HANDBACK ||
    sectionKind === SECTION_KIND.AUDIT
  );
}

export function excludeLifecycleSections(sections) {
  return (sections || []).filter(
    (section) => !isLifecycleSectionKind(resolveSectionKind(section))
  );
}

/** Whether this section may be skipped for the current form (ptw_form_section_map.skipable). */
export function isSectionSkipable(section) {
  if (!PTW_SECTION_SKIP_ENABLED) return false;
  if (resolveSectionKind(section) === SECTION_KIND.RISK) {
    return true;
  }
  const value = section?.skipable;
  return value === 1 || value === true || value === "1";
}
