"use client";

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

export interface UseConfigureStateOptions {
  plan: InternetPlanCatalogItem | null;
  installations: InternetInstallationCatalogItem[];
  addons: InternetAddonCatalogItem[];
  mode: AccessModeValue | null;
  selectedInstallation: InternetInstallationCatalogItem | null;
  currentStep: number;
  setCurrentStep: (step: number) => void;
}

/**
 * Hook for managing configuration wizard UI state (step navigation and transitions)
 * Now uses external currentStep from Zustand store for persistence
 *
 * @param options - Configuration state options
 * @param options.plan - Selected internet plan
 * @param options.installations - Available installation options
 * @param options.addons - Available addon options
 * @param options.mode - Currently selected access mode
 * @param options.selectedInstallation - Currently selected installation
 * @param options.currentStep - Current step from Zustand store
 * @param options.setCurrentStep - Step setter from Zustand store
 * @returns Step navigation helpers
 */
export function useConfigureState({
  plan,
  installations: _installations,
  addons: _addons,
  mode,
  selectedInstallation,
  currentStep,
  setCurrentStep,
}: UseConfigureStateOptions) {
  // 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,
  };
}
