import { computed } from "vue";

/**
 * Manage onboarding section navigation and section completion state.
 *
 * @param {object} options - Section refs and step predicates from the flow.
 * @returns {object} section navigation state and helpers.
 */
export function useOnboardingNavigation({ sections, activeSectionId, isStepDone, isStepRequired, isStepBlocked }) {
  const activeSection = computed(() => sections.value.find((item) => item.id === activeSectionId.value));

  const nextSectionItem = computed(() => {
    const list = sections.value;
    const index = list.findIndex((item) => item.id === activeSectionId.value);
    return index >= 0 ? list[index + 1] : null;
  });

  const hasPrevSection = computed(() => {
    const list = sections.value;
    const index = list.findIndex((item) => item.id === activeSectionId.value);
    return index > 0;
  });

  const hasNextSection = computed(() => Boolean(nextSectionItem.value));

  function selectSection(sectionId) {
    if (!sectionId) return;
    const section = sections.value.find((item) => item.id === sectionId);
    if (!section) return;
    if (isSectionLocked(section)) return;
    activeSectionId.value = sectionId;
  }

  function prevSection() {
    const list = sections.value;
    const index = list.findIndex((item) => item.id === activeSectionId.value);
    if (index > 0) {
      activeSectionId.value = list[index - 1].id;
    }
  }

  function nextSection() {
    const nextItem = nextSectionItem.value;
    if (nextItem && !isSectionLocked(nextItem)) {
      activeSectionId.value = nextItem.id;
    }
  }

  function stepCardClass(step) {
    return {
      done: isStepDone(step.id),
      blocked: isStepBlocked(step.id),
      optional: !isStepRequired(step.id),
    };
  }

  function sectionProgress(section) {
    const requiredSteps = section.steps.filter((step) => isStepRequired(step.id));
    if (!requiredSteps.length) return "optional";
    if (isSectionLocked(section)) return `0/${requiredSteps.length} done`;
    const doneCount = requiredSteps.filter((step) => isStepDone(step.id) && !isStepBlocked(step.id)).length;
    return `${doneCount}/${requiredSteps.length} done`;
  }

  function sectionStatusLabel(section) {
    if (isSectionDone(section)) return "";
    if (isSectionLocked(section)) return "locked";
    return "active";
  }

  function sectionPillClass(section) {
    if (isSectionLocked(section)) return "pill-wait";
    return "pill-info";
  }

  function isSectionLocked(section) {
    const list = sections.value;
    const index = list.findIndex((item) => item.id === section.id);
    if (index <= 0) return false;
    const previous = list[index - 1];
    return !sectionGateComplete(previous);
  }

  function isSectionDone(section) {
    const requiredSteps = section.steps.filter((step) => isStepRequired(step.id));
    const stepsToCheck = requiredSteps.length ? requiredSteps : section.steps;
    if (!stepsToCheck.length) return false;
    return stepsToCheck.every((step) => isStepDone(step.id));
  }

  function sectionCardClass(section) {
    return {
      active: section.id === activeSectionId.value,
      done: isSectionDone(section),
      locked: isSectionLocked(section),
    };
  }

  function sectionGateComplete(section) {
    const requiredSteps = section.steps.filter((step) => isStepRequired(step.id));
    if (!requiredSteps.length) return true;
    return requiredSteps.every((step) => isStepDone(step.id));
  }


  return {
    activeSection,
    nextSectionItem,
    hasPrevSection,
    hasNextSection,
    selectSection,
    prevSection,
    nextSection,
    stepCardClass,
    sectionProgress,
    sectionStatusLabel,
    sectionPillClass,
    isSectionLocked,
    isSectionDone,
    sectionCardClass,
    sectionGateComplete,
  };
}
