/**
 * ComplianceContext — React context + value shape for ComplianceProvider.
 *
 * Phase 4: extracted from provider.tsx so the provider module stays under
 * the file-size cap.
 */

import { createContext } from 'react'
import type {
  ComplianceClient,
  DSGVOModule,
  ComplianceModule,
  RAGModule,
  SecurityModule,
} from '@breakpilot/compliance-sdk-core'
import type {
  SDKState,
  SDKAction,
  SDKStep,
  CheckpointStatus,
  SyncState,
  UseCaseAssessment,
  Risk,
  Control,
} from '@breakpilot/compliance-sdk-types'

// Shared localStorage key prefix for provider persistence.
export const SDK_STORAGE_KEY = 'breakpilot-compliance-sdk-state'

export interface ComplianceContextValue {
  // State
  state: SDKState
  dispatch: React.Dispatch<SDKAction>

  // Client
  client: ComplianceClient

  // Modules
  dsgvo: DSGVOModule
  compliance: ComplianceModule
  rag: RAGModule
  security: SecurityModule

  // Navigation
  currentStep: SDKStep | undefined
  goToStep: (stepId: string) => void
  goToNextStep: () => void
  goToPreviousStep: () => void
  canGoNext: boolean
  canGoPrevious: boolean

  // Progress
  completionPercentage: number
  phase1Completion: number
  phase2Completion: number

  // Checkpoints
  validateCheckpoint: (checkpointId: string) => Promise<CheckpointStatus>
  overrideCheckpoint: (checkpointId: string, reason: string) => Promise<void>
  getCheckpointStatus: (checkpointId: string) => CheckpointStatus | undefined

  // State Updates
  updateUseCase: (id: string, data: Partial<UseCaseAssessment>) => void
  addRisk: (risk: Risk) => void
  updateControl: (id: string, data: Partial<Control>) => void

  // Persistence
  saveState: () => Promise<void>
  loadState: () => Promise<void>
  resetState: () => void

  // Sync
  syncState: SyncState
  forceSyncToServer: () => Promise<void>
  isOnline: boolean

  // Export
  exportState: (format: 'json' | 'pdf' | 'zip') => Promise<Blob>

  // Command Bar
  isCommandBarOpen: boolean
  setCommandBarOpen: (open: boolean) => void

  // Status
  isInitialized: boolean
  isLoading: boolean
  error: Error | null
}

export const ComplianceContext = createContext<ComplianceContextValue | null>(null)

export interface ComplianceProviderProps {
  children: React.ReactNode
  apiEndpoint: string
  apiKey?: string
  tenantId: string
  userId?: string
  enableBackendSync?: boolean
  onNavigate?: (url: string) => void
  onError?: (error: Error) => void
}
