'use client'

import { useMemo } from 'react'
import { useCompliance } from './hooks-core'

// =============================================================================
// NAVIGATION HOOKS
// =============================================================================

export function useSDKNavigation() {
  const {
    currentStep,
    goToStep,
    goToNextStep,
    goToPreviousStep,
    canGoNext,
    canGoPrevious,
    completionPercentage,
    phase1Completion,
    phase2Completion,
    state,
  } = useCompliance()

  return useMemo(
    () => ({
      currentStep,
      currentPhase: state.currentPhase,
      completedSteps: state.completedSteps,
      goToStep,
      goToNextStep,
      goToPreviousStep,
      canGoNext,
      canGoPrevious,
      completionPercentage,
      phase1Completion,
      phase2Completion,
    }),
    [
      currentStep,
      state.currentPhase,
      state.completedSteps,
      goToStep,
      goToNextStep,
      goToPreviousStep,
      canGoNext,
      canGoPrevious,
      completionPercentage,
      phase1Completion,
      phase2Completion,
    ]
  )
}

// =============================================================================
// SYNC HOOKS
// =============================================================================

export function useSync() {
  const { syncState, forceSyncToServer, isOnline, saveState, loadState } = useCompliance()

  return useMemo(
    () => ({
      status: syncState.status,
      lastSyncedAt: syncState.lastSyncedAt,
      pendingChanges: syncState.pendingChanges,
      error: syncState.error,
      isOnline,
      isSyncing: syncState.status === 'syncing',
      hasConflict: syncState.status === 'conflict',
      forceSyncToServer,
      saveState,
      loadState,
    }),
    [syncState, isOnline, forceSyncToServer, saveState, loadState]
  )
}

// =============================================================================
// CHECKPOINT HOOKS
// =============================================================================

export function useCheckpoints() {
  const { validateCheckpoint, overrideCheckpoint, getCheckpointStatus, state } = useCompliance()

  return useMemo(
    () => ({
      checkpoints: state.checkpoints,
      validateCheckpoint,
      overrideCheckpoint,
      getCheckpointStatus,
      passedCheckpoints: Object.values(state.checkpoints).filter(c => c.passed).length,
      totalCheckpoints: Object.keys(state.checkpoints).length,
    }),
    [state.checkpoints, validateCheckpoint, overrideCheckpoint, getCheckpointStatus]
  )
}

// =============================================================================
// EXPORT HOOKS
// =============================================================================

export function useExport() {
  const { exportState } = useCompliance()

  return useMemo(
    () => ({
      exportJSON: () => exportState('json'),
      exportPDF: () => exportState('pdf'),
      exportZIP: () => exportState('zip'),
      exportState,
    }),
    [exportState]
  )
}

// =============================================================================
// COMMAND BAR HOOK
// =============================================================================

export function useCommandBar() {
  const { isCommandBarOpen, setCommandBarOpen } = useCompliance()

  return useMemo(
    () => ({
      isOpen: isCommandBarOpen,
      open: () => setCommandBarOpen(true),
      close: () => setCommandBarOpen(false),
      toggle: () => setCommandBarOpen(!isCommandBarOpen),
    }),
    [isCommandBarOpen, setCommandBarOpen]
  )
}
