/**
 * Base Types and Enums
 *
 * Core type definitions used across all SDK modules
 */

// =============================================================================
// SUBSCRIPTION & PLANS
// =============================================================================

export type SubscriptionTier = 'FREE' | 'STARTER' | 'PROFESSIONAL' | 'ENTERPRISE'

export type DeploymentMode = 'CLOUD' | 'MAC_MINI' | 'MAC_STUDIO' | 'ON_PREMISE'

// =============================================================================
// STATUS ENUMS
// =============================================================================

export type ValidationSeverity = 'ERROR' | 'WARNING' | 'INFO'

export type RiskSeverity = 'LOW' | 'MEDIUM' | 'HIGH' | 'CRITICAL'

export type RiskLikelihood = 1 | 2 | 3 | 4 | 5

export type RiskImpact = 1 | 2 | 3 | 4 | 5

export type ImplementationStatus = 'NOT_IMPLEMENTED' | 'PARTIAL' | 'IMPLEMENTED'

export type RequirementStatus = 'NOT_STARTED' | 'IN_PROGRESS' | 'IMPLEMENTED' | 'VERIFIED'

export type ReviewerType = 'NONE' | 'TEAM_LEAD' | 'DSB' | 'LEGAL'

// =============================================================================
// TENANT & USER
// =============================================================================

export interface Tenant {
  id: string
  name: string
  subscription: SubscriptionTier
  deploymentMode: DeploymentMode
  createdAt: Date
  settings: TenantSettings
}

export interface TenantSettings {
  defaultLanguage: 'de' | 'en'
  timezone: string
  dataResidency: 'EU' | 'DE' | 'CUSTOM'
  features: EnabledFeatures
}

export interface EnabledFeatures {
  dsgvo: boolean
  nis2: boolean
  aiAct: boolean
  sbom: boolean
  security: boolean
  rag: boolean
}

export interface User {
  id: string
  tenantId: string
  email: string
  name: string
  role: UserRole
  permissions: Permission[]
  preferences: UserPreferences
}

export type UserRole = 'ADMIN' | 'DSB' | 'COMPLIANCE_MANAGER' | 'AUDITOR' | 'VIEWER'

export type Permission =
  | 'READ_STATE'
  | 'WRITE_STATE'
  | 'MANAGE_CONTROLS'
  | 'MANAGE_EVIDENCE'
  | 'APPROVE_DSFA'
  | 'EXPORT_DATA'
  | 'MANAGE_USERS'
  | 'MANAGE_SETTINGS'

export interface UserPreferences {
  language: 'de' | 'en'
  theme: 'light' | 'dark' | 'system'
  compactMode: boolean
  showHints: boolean
  autoSave: boolean
  autoValidate: boolean
}

// =============================================================================
// VALIDATION & CHECKPOINTS
// =============================================================================

export type CheckpointType = 'REQUIRED' | 'RECOMMENDED' | 'OPTIONAL'

export interface ValidationRule {
  id: string
  field: string
  condition: 'NOT_EMPTY' | 'MIN_COUNT' | 'MIN_VALUE' | 'CUSTOM' | 'REGEX'
  value?: number | string
  message: string
  severity: ValidationSeverity
}

export interface ValidationError {
  ruleId: string
  field: string
  message: string
  severity: ValidationSeverity
}

export interface Checkpoint {
  id: string
  step: string
  name: string
  type: CheckpointType
  validation: ValidationRule[]
  blocksProgress: boolean
  requiresReview: ReviewerType
  autoValidate: boolean
}

export interface CheckpointStatus {
  checkpointId: string
  passed: boolean
  validatedAt: Date | null
  validatedBy: string | null
  errors: ValidationError[]
  warnings: ValidationError[]
  overrideReason?: string
  overriddenBy?: string
  overriddenAt?: Date
}

// =============================================================================
// RISK MANAGEMENT
// =============================================================================

export type RiskStatus = 'IDENTIFIED' | 'ASSESSED' | 'MITIGATED' | 'ACCEPTED' | 'CLOSED'

export type MitigationType = 'AVOID' | 'TRANSFER' | 'MITIGATE' | 'ACCEPT'

export interface RiskMitigation {
  id: string
  description: string
  type: MitigationType
  status: 'PLANNED' | 'IN_PROGRESS' | 'COMPLETED'
  effectiveness: number // 0-100
  controlId: string | null
}

export interface Risk {
  id: string
  title: string
  description: string
  category: string
  likelihood: RiskLikelihood
  impact: RiskImpact
  severity: RiskSeverity
  inherentRiskScore: number
  residualRiskScore: number
  status: RiskStatus
  mitigation: RiskMitigation[]
  owner: string | null
  relatedControls: string[]
  relatedRequirements: string[]
}

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

export type CommandType = 'ACTION' | 'NAVIGATION' | 'SEARCH' | 'GENERATE' | 'HELP'

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
}

// =============================================================================
// UTILITY FUNCTIONS
// =============================================================================

export function calculateRiskScore(likelihood: RiskLikelihood, impact: RiskImpact): number {
  return likelihood * impact
}

export function getRiskSeverityFromScore(score: number): RiskSeverity {
  if (score >= 20) return 'CRITICAL'
  if (score >= 12) return 'HIGH'
  if (score >= 6) return 'MEDIUM'
  return 'LOW'
}

export function calculateResidualRisk(risk: Risk): number {
  const inherentScore = calculateRiskScore(risk.likelihood, risk.impact)
  const totalEffectiveness = risk.mitigation
    .filter(m => m.status === 'COMPLETED')
    .reduce((sum, m) => sum + m.effectiveness, 0)

  const effectivenessMultiplier = Math.min(totalEffectiveness, 100) / 100
  return Math.max(1, Math.round(inherentScore * (1 - effectivenessMultiplier)))
}
