/**
 * SDK State & Actions
 *
 * Central SDK state interface, action discriminated union,
 * user preferences, and command bar types.
 */

import type {
  SubscriptionTier,
  SDKPhase,
  CustomerType,
  CommandType,
} from './core'
import type { CompanyProfile } from './company-profile'
import type { CheckpointStatus } from './checkpoint'
import type { UseCaseAssessment } from './assessment'
import type { ScreeningResult, SecurityIssue, BacklogItem, SBOM } from './screening-security'
import type { ServiceModule, Requirement, Control, Evidence, ChecklistItem } from './compliance'
import type { Risk } from './risk'
import type { AIActResult, Obligation } from './ai-act-obligations'
import type { DSFA } from './dsfa'
import type {
  TOM,
  RetentionPolicy,
  ProcessingActivity,
  LegalDocument,
  CookieBannerConfig,
  ConsentRecord,
  DSRConfig,
  ImportedDocument,
  GapAnalysis,
  EscalationWorkflow,
} from './documentation'

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

export interface CommandSuggestion {
  id: string
  type: CommandType
  label: string
  description: string
  shortcut?: string
  icon?: string
  action: () => void | Promise<void>
  relevanceScore: number
}

export interface CommandHistory {
  id: string
  query: string
  type: CommandType
  timestamp: Date
  success: boolean
}

// =============================================================================
// USER PREFERENCES
// =============================================================================

export interface UserPreferences {
  language: 'de' | 'en'
  theme: 'light' | 'dark' | 'system'
  compactMode: boolean
  showHints: boolean
  autoSave: boolean
  autoValidate: boolean
  allowParallelWork: boolean // Erlaubt Navigation zu allen Schritten ohne Voraussetzungen
}

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

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

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

  // Customer Type (new vs existing)
  customerType: CustomerType | null

  // Company Profile (collected before use cases)
  companyProfile: CompanyProfile | null

  // Compliance Scope (determines depth level L1-L4)
  complianceScope: import('../compliance-scope-types').ComplianceScopeState | null

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

  // Imported Documents (for existing customers)
  importedDocuments: ImportedDocument[]
  gapAnalysis: GapAnalysis | null

  // 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
  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: 'SET_CUSTOMER_TYPE'; payload: CustomerType }
  | { type: 'SET_COMPANY_PROFILE'; payload: CompanyProfile }
  | { type: 'UPDATE_COMPANY_PROFILE'; payload: Partial<CompanyProfile> }
  | { type: 'SET_COMPLIANCE_SCOPE'; payload: import('../compliance-scope-types').ComplianceScopeState }
  | { type: 'UPDATE_COMPLIANCE_SCOPE'; payload: Partial<import('../compliance-scope-types').ComplianceScopeState> }
  | { type: 'ADD_IMPORTED_DOCUMENT'; payload: ImportedDocument }
  | { type: 'UPDATE_IMPORTED_DOCUMENT'; payload: { id: string; data: Partial<ImportedDocument> } }
  | { type: 'DELETE_IMPORTED_DOCUMENT'; payload: string }
  | { type: 'SET_GAP_ANALYSIS'; payload: GapAnalysis }
  | { 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_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' }
