import { useMemo } from 'react';

import { t } from '@grafana/i18n';

import { type Step } from '../Stepper';
import { type GitHubAppMode, type GitHubAuthType, type WizardStep } from '../types';

export interface UseWizardButtonsParams {
  activeStep: WizardStep;
  steps: Array<Step<WizardStep>>;
  repoName: string;
  canSkipSync: boolean;
  isSubmitting: boolean;
  isCancelling: boolean;
  isStepRunning: boolean;
  isStepSuccess: boolean;
  hasStepError: boolean;
  hasStepWarning: boolean;
  isCreatingSkipJob: boolean;
  showCancelConfirmation: boolean;
  shouldUseCancelBehavior: boolean;
  githubAppMode?: GitHubAppMode;
  githubAuthType?: GitHubAuthType;
}

export interface UseWizardButtonsReturn {
  nextButtonText: string;
  previousButtonText: string;
  isNextDisabled: boolean;
  isPreviousDisabled: boolean;
}

export function useWizardButtons({
  activeStep,
  steps,
  repoName,
  canSkipSync,
  isSubmitting,
  isCancelling,
  isStepRunning,
  isStepSuccess,
  hasStepError,
  hasStepWarning,
  isCreatingSkipJob,
  showCancelConfirmation,
  shouldUseCancelBehavior,
  githubAppMode,
  githubAuthType,
}: UseWizardButtonsParams): UseWizardButtonsReturn {
  const nextButtonText = useMemo(() => {
    const stepIndex = steps.findIndex((s) => s.id === activeStep);

    if (stepIndex === -1 || stepIndex >= steps.length - 1) {
      return t('provisioning.wizard.button-next', 'Finish');
    }

    // If on bootstrap step and should skip sync, show finish step name
    if (activeStep === 'bootstrap' && canSkipSync) {
      const finishStepIndex = stepIndex + 2;
      if (finishStepIndex < steps.length) {
        return steps[finishStepIndex].name;
      }
      return t('provisioning.wizard.button-next', 'Finish');
    }

    return steps[stepIndex + 1].name;
  }, [activeStep, steps, canSkipSync]);

  const previousButtonText = useMemo(() => {
    if (isCancelling) {
      return t('provisioning.wizard-content.button-cancelling', 'Cancelling...');
    }

    if (shouldUseCancelBehavior || (activeStep === 'connection' && repoName)) {
      return t('provisioning.wizard-content.button-cancel', 'Cancel');
    }

    return t('provisioning.wizard-content.button-previous', 'Previous');
  }, [isCancelling, shouldUseCancelBehavior, activeStep, repoName]);

  const isNextDisabled = useMemo(() => {
    if (activeStep === 'authType') {
      // Only disable next if github app is in create connection mode
      return githubAuthType === 'github-app' && githubAppMode === 'new';
    }
    // Don't block on hasStepError for connection steps - users can fix their input and retry
    if (!['connection'].includes(activeStep) && hasStepError) {
      return true;
    }
    // Synchronize step requires success or warning to proceed
    if (activeStep === 'synchronize') {
      return !(isStepSuccess || hasStepWarning);
    }
    // Finish step should not be blocked by isCreatingSkipJob since we only
    // reach finish after the skip job was successfully created
    if (activeStep === 'finish') {
      return isSubmitting || isCancelling;
    }
    return isSubmitting || isCancelling || isStepRunning || isCreatingSkipJob;
  }, [
    activeStep,
    hasStepError,
    isStepSuccess,
    hasStepWarning,
    isSubmitting,
    isCancelling,
    isStepRunning,
    isCreatingSkipJob,
    githubAuthType,
    githubAppMode,
  ]);
  const isPreviousDisabled = isSubmitting || isCancelling || isStepRunning || showCancelConfirmation;

  return {
    nextButtonText,
    previousButtonText,
    isNextDisabled,
    isPreviousDisabled,
  };
}
