/**
 * SDKState interface + SDKAction union + completion-percentage helpers.
 *
 * Phase 4: extracted from state.ts.
 */

import type {
  SubscriptionTier,
  UserPreferences,
  Risk,
  CommandHistory,
  CheckpointStatus,
} from './base'
import type {
  ConsentRecord,
  DSRConfig,
  DSRRequest,
  CookieBannerConfig,
  ProcessingActivity,
  DSFA,
  TOM,
  RetentionPolicy,
  EscalationWorkflow,
  LegalDocument,
} from './dsgvo'
import type {
  Control,
  Evidence,
  Requirement,
  ServiceModule,
  Obligation,
  AIActResult,
  ChecklistItem,
} from './compliance'
import type { SBOM, SecurityIssue, BacklogItem } from './security'
import type { UseCaseAssessment, ScreeningResult } from './state-assessment'
import { SDK_STEPS, getStepsForPhase, type SDKPhase } from './state-flow'

// =============================================================================
// SDK STATE
// =============================================================================

export interface SDKState {
  // Metadata
  version: string
  lastModified: Date

  // Tenant & User
  tenantId: string
  userId: string
  subscription: SubscriptionTier

  // Progress
  currentPhase: SDKPhase
  currentStep: string
  completedSteps: string[]
  checkpoints: Record<string, CheckpointStatus>

  // Phase 1 Data
  useCases: UseCaseAssessment[]
  activeUseCase: string | null
  screening: ScreeningResult | null
  modules: ServiceModule[]
  requirements: Requirement[]
  controls: Control[]
  evidence: Evidence[]
  checklist: ChecklistItem[]
  risks: Risk[]

  // Phase 2 Data
  aiActClassification: AIActResult | null
  obligations: Obligation[]
  dsfa: DSFA | null
  toms: TOM[]
  retentionPolicies: RetentionPolicy[]
  vvt: ProcessingActivity[]
  documents: LegalDocument[]
  cookieBanner: CookieBannerConfig | null
  consents: ConsentRecord[]
  dsrConfig: DSRConfig | null
  dsrRequests: DSRRequest[]
  escalationWorkflows: EscalationWorkflow[]

  // Security
  sbom: SBOM | null
  securityIssues: SecurityIssue[]
  securityBacklog: BacklogItem[]

  // UI State
  commandBarHistory: CommandHistory[]
  recentSearches: string[]
  preferences: UserPreferences
}

// =============================================================================
// SDK ACTIONS
// =============================================================================

export type SDKAction =
  | { type: 'SET_STATE'; payload: Partial<SDKState> }
  | { type: 'SET_CURRENT_STEP'; payload: string }
  | { type: 'COMPLETE_STEP'; payload: string }
  | { type: 'SET_CHECKPOINT_STATUS'; payload: { id: string; status: CheckpointStatus } }
  | { type: 'ADD_USE_CASE'; payload: UseCaseAssessment }
  | { type: 'UPDATE_USE_CASE'; payload: { id: string; data: Partial<UseCaseAssessment> } }
  | { type: 'DELETE_USE_CASE'; payload: string }
  | { type: 'SET_ACTIVE_USE_CASE'; payload: string | null }
  | { type: 'SET_SCREENING'; payload: ScreeningResult }
  | { type: 'ADD_MODULE'; payload: ServiceModule }
  | { type: 'UPDATE_MODULE'; payload: { id: string; data: Partial<ServiceModule> } }
  | { type: 'ADD_REQUIREMENT'; payload: Requirement }
  | { type: 'UPDATE_REQUIREMENT'; payload: { id: string; data: Partial<Requirement> } }
  | { type: 'ADD_CONTROL'; payload: Control }
  | { type: 'UPDATE_CONTROL'; payload: { id: string; data: Partial<Control> } }
  | { type: 'ADD_EVIDENCE'; payload: Evidence }
  | { type: 'UPDATE_EVIDENCE'; payload: { id: string; data: Partial<Evidence> } }
  | { type: 'DELETE_EVIDENCE'; payload: string }
  | { type: 'ADD_RISK'; payload: Risk }
  | { type: 'UPDATE_RISK'; payload: { id: string; data: Partial<Risk> } }
  | { type: 'DELETE_RISK'; payload: string }
  | { type: 'SET_AI_ACT_RESULT'; payload: AIActResult }
  | { type: 'ADD_OBLIGATION'; payload: Obligation }
  | { type: 'UPDATE_OBLIGATION'; payload: { id: string; data: Partial<Obligation> } }
  | { type: 'SET_DSFA'; payload: DSFA }
  | { type: 'ADD_TOM'; payload: TOM }
  | { type: 'UPDATE_TOM'; payload: { id: string; data: Partial<TOM> } }
  | { type: 'ADD_RETENTION_POLICY'; payload: RetentionPolicy }
  | { type: 'UPDATE_RETENTION_POLICY'; payload: { id: string; data: Partial<RetentionPolicy> } }
  | { type: 'ADD_PROCESSING_ACTIVITY'; payload: ProcessingActivity }
  | { type: 'UPDATE_PROCESSING_ACTIVITY'; payload: { id: string; data: Partial<ProcessingActivity> } }
  | { type: 'ADD_DOCUMENT'; payload: LegalDocument }
  | { type: 'UPDATE_DOCUMENT'; payload: { id: string; data: Partial<LegalDocument> } }
  | { type: 'SET_COOKIE_BANNER'; payload: CookieBannerConfig }
  | { type: 'SET_DSR_CONFIG'; payload: DSRConfig }
  | { type: 'ADD_DSR_REQUEST'; payload: DSRRequest }
  | { type: 'UPDATE_DSR_REQUEST'; payload: { id: string; data: Partial<DSRRequest> } }
  | { type: 'ADD_ESCALATION_WORKFLOW'; payload: EscalationWorkflow }
  | { type: 'UPDATE_ESCALATION_WORKFLOW'; payload: { id: string; data: Partial<EscalationWorkflow> } }
  | { type: 'ADD_SECURITY_ISSUE'; payload: SecurityIssue }
  | { type: 'UPDATE_SECURITY_ISSUE'; payload: { id: string; data: Partial<SecurityIssue> } }
  | { type: 'ADD_BACKLOG_ITEM'; payload: BacklogItem }
  | { type: 'UPDATE_BACKLOG_ITEM'; payload: { id: string; data: Partial<BacklogItem> } }
  | { type: 'ADD_COMMAND_HISTORY'; payload: CommandHistory }
  | { type: 'SET_PREFERENCES'; payload: Partial<UserPreferences> }
  | { type: 'RESET_STATE' }

// =============================================================================
// COMPLETION HELPERS
// =============================================================================

export function getCompletionPercentage(state: SDKState): number {
  const totalSteps = SDK_STEPS.length
  const completedSteps = state.completedSteps.length
  return Math.round((completedSteps / totalSteps) * 100)
}

export function getPhaseCompletionPercentage(state: SDKState, phase: SDKPhase): number {
  const phaseSteps = getStepsForPhase(phase)
  const completedPhaseSteps = phaseSteps.filter(s => state.completedSteps.includes(s.id))
  return Math.round((completedPhaseSteps.length / phaseSteps.length) * 100)
}
