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

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

import { type ReviewInputSectionType } from '../../../components/ReviewInputSection';
import { type CalibrationBuckets } from '../types';
import {
  buildSectionTabs,
  buildStepperSteps,
  getAvailableSectionTypes,
  getQuestionsForSection,
} from '../utils';

type UseCalibrationFormStepsParams = {
  buckets: CalibrationBuckets;
  t: LokaliseTFunction;
};

type UseCalibrationFormStepsResult = {
  availableSectionTypes: ReviewInputSectionType[];
  activeSectionType: ReviewInputSectionType | undefined;
  currentStepIndex: number;
  isFirstStep: boolean;
  isLastStep: boolean;
  sectionTabsForHu: ReturnType<typeof buildSectionTabs>;
  stepperSteps: ReturnType<typeof buildStepperSteps>;
  currentStepQuestions: ReviewDetailFormQuestions[];
  stepHasRequiredQuestion: boolean;
  goPrev: () => void;
  goNext: () => void;
  handleTabChange: (value: string | number) => void;
};

/**
 * Owns the step / section navigation state of the calibration form: which
 * sections exist, which one is active, navigation handlers, and the translated
 * payloads consumed by `HuStepper` and `HuTabs`.
 */
export const useCalibrationFormSteps = ({
  buckets,
  t,
}: UseCalibrationFormStepsParams): UseCalibrationFormStepsResult => {
  const availableSectionTypes = useMemo(
    () => getAvailableSectionTypes(buckets),
    [buckets],
  );

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

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

  const sectionTabsForHu = useMemo(
    () => buildSectionTabs(availableSectionTypes, t),
    [availableSectionTypes, t],
  );

  const stepperSteps = useMemo(
    () => buildStepperSteps(availableSectionTypes, t),
    [availableSectionTypes, t],
  );

  const activeSectionType =
    availableSectionTypes[currentStepIndex] ?? availableSectionTypes[0];

  const isLastStep =
    availableSectionTypes.length <= 1 ||
    currentStepIndex === availableSectionTypes.length - 1;

  const isFirstStep = currentStepIndex === 0;

  const currentStepQuestions = useMemo(
    () => getQuestionsForSection(activeSectionType, buckets),
    [activeSectionType, buckets],
  );

  const stepHasRequiredQuestion = useMemo(
    () => currentStepQuestions.some(question => question.required),
    [currentStepQuestions],
  );

  const goPrev = useCallback(() => setCurrentStepIndex(prev => prev - 1), []);
  const goNext = useCallback(() => setCurrentStepIndex(prev => prev + 1), []);

  const handleTabChange = useCallback(
    (value: string | number) => {
      const idx = availableSectionTypes.indexOf(
        value as ReviewInputSectionType,
      );
      if (idx >= 0) setCurrentStepIndex(idx);
    },
    [availableSectionTypes],
  );

  return {
    availableSectionTypes,
    activeSectionType,
    currentStepIndex,
    isFirstStep,
    isLastStep,
    sectionTabsForHu,
    stepperSteps,
    currentStepQuestions,
    stepHasRequiredQuestion,
    goPrev,
    goNext,
    handleTabChange,
  };
};
