import { useEffect, useMemo, useState } from 'react';

import {
  type ReviewDetailFormQuestions,
  type ReviewDetailFormQuestionsGoalItem,
} from 'src/pages/dashboard/performance/types';
import { type LokaliseTFunction } from 'src/utils/i18n';

import {
  ReviewInputSectionType,
  reviewSectionTitleTranslationKey,
} from '../../../../components/ReviewInputSection';

type UseReviewStepsParams = {
  formQuestions: ReviewDetailFormQuestions[];
  goalQuestions: ReviewDetailFormQuestionsGoalItem[];
  competencyQuestions: ReviewDetailFormQuestions[];
  t: LokaliseTFunction;
};

/**
 * Encapsulates the step/tab navigation state of the review form:
 * which sections are available, which one is active, and helpers to move forward/back.
 *
 * Also scrolls the review layout container to the top whenever the active step changes.
 */
const useReviewSteps = ({
  formQuestions,
  goalQuestions,
  competencyQuestions,
  t,
}: UseReviewStepsParams) => {
  const availableTabs = useMemo(() => {
    const tabs: ReviewInputSectionType[] = [];
    if (formQuestions.length > 0) tabs.push(ReviewInputSectionType.FORM);
    if (goalQuestions.length > 0) tabs.push(ReviewInputSectionType.GOAL);
    if (competencyQuestions.length > 0)
      tabs.push(ReviewInputSectionType.COMPETENCY);
    return tabs;
  }, [formQuestions.length, goalQuestions.length, competencyQuestions.length]);

  const [currentStepIndex, setCurrentStepIndex] = useState(0);

  useEffect(() => {
    if (availableTabs.length === 0) return;
    setCurrentStepIndex(prev => (prev >= availableTabs.length ? 0 : prev));
  }, [availableTabs]);

  // biome-ignore lint/correctness/useExhaustiveDependencies: scroll on step change
  useEffect(() => {
    document.getElementById('review-layout')?.scrollTo(0, 0);
  }, [currentStepIndex]);

  const sectionTabs = useMemo(
    () =>
      availableTabs.map(value => ({
        value,
        label: t(reviewSectionTitleTranslationKey[value]),
      })),
    [availableTabs, t],
  );

  const stepperSteps = useMemo(
    () =>
      availableTabs.map((tab, index) => ({
        id: tab,
        title: t(reviewSectionTitleTranslationKey[tab]),
        number: index + 1,
      })),
    [availableTabs, t],
  );

  const activeSectionType = availableTabs[currentStepIndex] ?? availableTabs[0];
  const isLastStep =
    availableTabs.length <= 1 || currentStepIndex === availableTabs.length - 1;
  const isFirstStep = currentStepIndex === 0;

  const currentStepQuestions = useMemo(() => {
    switch (activeSectionType) {
      case ReviewInputSectionType.FORM:
        return formQuestions;
      case ReviewInputSectionType.GOAL:
        return goalQuestions;
      case ReviewInputSectionType.COMPETENCY:
        return competencyQuestions;
      default:
        return [];
    }
  }, [activeSectionType, formQuestions, goalQuestions, competencyQuestions]);

  const goNext = () => setCurrentStepIndex(prev => prev + 1);
  const goBack = () => setCurrentStepIndex(prev => prev - 1);
  const goToTab = (section: ReviewInputSectionType) => {
    const idx = availableTabs.indexOf(section);
    if (idx >= 0) setCurrentStepIndex(idx);
  };

  return {
    availableTabs,
    sectionTabs,
    stepperSteps,
    currentStepIndex,
    activeSectionType,
    currentStepQuestions,
    isFirstStep,
    isLastStep,
    goNext,
    goBack,
    goToTab,
  };
};

export default useReviewSteps;
