"use client";

import { useCallback } from "react";
import type {
  InternetPlanCatalogItem,
  InternetInstallationCatalogItem,
  InternetAddonCatalogItem,
} from "@customer-portal/domain/catalog";
import type { AccessModeValue } from "@customer-portal/domain/orders";

/**
 * Hook for managing configuration wizard UI state (step navigation and transitions)
 * Now uses external currentStep from Zustand store for persistence
 *
 * @param plan - Selected internet plan
 * @param installations - Available installation options
 * @param addons - Available addon options
 * @param mode - Currently selected access mode
 * @param selectedInstallation - Currently selected installation
 * @param currentStep - Current step from Zustand store
 * @param setCurrentStep - Step setter from Zustand store
 * @returns Step navigation helpers
 */
export function useConfigureState(
  plan: InternetPlanCatalogItem | null,
  installations: InternetInstallationCatalogItem[],
  addons: InternetAddonCatalogItem[],
  mode: AccessModeValue | null,
  selectedInstallation: InternetInstallationCatalogItem | null,
  currentStep: number,
  setCurrentStep: (step: number) => void
) {
  // UI validation - determines if user can proceed from current step
  // Note: Real validation should happen on BFF during order submission
  const canProceedFromStep = useCallback(
    (step: number): boolean => {
      switch (step) {
        case 1:
          // Silver plans require explicit mode selection
          // Gold/Platinum have default mode
          if (plan?.internetPlanTier === "Silver") {
            return mode !== null;
          }
          return true;
        case 2:
          // Installation selection is required
          return selectedInstallation !== null;
        case 3:
          // Add-ons are optional
          return true;
        case 4:
          // Review step - always can proceed
          return true;
        default:
          return false;
      }
    },
    [plan, mode, selectedInstallation]
  );

  return {
    currentStep,
    canProceedFromStep,
    setCurrentStep,
  };
}
