/**
 * GCI (Gesamt-Compliance-Index) Types
 * TypeScript definitions for the 4-level compliance scoring model
 */

// =============================================================================
// MATURITY LEVELS
// =============================================================================

export type MaturityLevel = 'OPTIMIZED' | 'MANAGED' | 'DEFINED' | 'REACTIVE' | 'HIGH_RISK'

export const MATURITY_INFO: Record<MaturityLevel, { label: string; color: string; bgColor: string; borderColor: string; description: string }> = {
  OPTIMIZED: { label: 'Optimiert', color: 'text-green-700', bgColor: 'bg-green-100', borderColor: 'border-green-300', description: 'Kontinuierliche Verbesserung, proaktive Compliance' },
  MANAGED: { label: 'Gesteuert', color: 'text-blue-700', bgColor: 'bg-blue-100', borderColor: 'border-blue-300', description: 'Messbare Prozesse, regelmaessige Reviews' },
  DEFINED: { label: 'Definiert', color: 'text-yellow-700', bgColor: 'bg-yellow-100', borderColor: 'border-yellow-300', description: 'Dokumentierte Prozesse, erste Strukturen' },
  REACTIVE: { label: 'Reaktiv', color: 'text-orange-700', bgColor: 'bg-orange-100', borderColor: 'border-orange-300', description: 'Ad-hoc Massnahmen, wenig Struktur' },
  HIGH_RISK: { label: 'Hohes Risiko', color: 'text-red-700', bgColor: 'bg-red-100', borderColor: 'border-red-300', description: 'Erheblicher Handlungsbedarf, Compliance-Luecken' },
}

// =============================================================================
// LEVEL 1: MODULE SCORE
// =============================================================================

export interface ModuleScore {
  module_id: string
  module_name: string
  assigned: number
  completed: number
  raw_score: number
  validity_factor: number
  final_score: number
  risk_weight: number
  category: string
}

// =============================================================================
// LEVEL 2: RISK-WEIGHTED AREA SCORE
// =============================================================================

export interface RiskWeightedScore {
  area_id: string
  area_name: string
  modules: ModuleScore[]
  weighted_sum: number
  total_weight: number
  area_score: number
}

// =============================================================================
// LEVEL 3: REGULATION AREA SCORE
// =============================================================================

export interface RegulationAreaScore {
  regulation_id: string
  regulation_name: string
  score: number
  weight: number
  weighted_score: number
  module_count: number
  completed_count: number
}

// =============================================================================
// LEVEL 4: GCI RESULT
// =============================================================================

export interface AuditEntry {
  timestamp: string
  factor: string
  description: string
  value: number
  impact: 'positive' | 'negative' | 'neutral'
}

export interface GCIResult {
  tenant_id: string
  gci_score: number
  maturity_level: MaturityLevel
  maturity_label: string
  calculated_at: string
  profile: string
  area_scores: RegulationAreaScore[]
  criticality_multiplier: number
  incident_adjustment: number
  audit_trail: AuditEntry[]
}

export interface GCIBreakdown extends GCIResult {
  level1_modules: ModuleScore[]
  level2_areas: RiskWeightedScore[]
}

// =============================================================================
// GCI HISTORY
// =============================================================================

export interface GCISnapshot {
  tenant_id: string
  score: number
  maturity_level: MaturityLevel
  area_scores: Record<string, number>
  calculated_at: string
}

export interface GCIHistoryResponse {
  tenant_id: string
  snapshots: GCISnapshot[]
  total: number
}

// =============================================================================
// COMPLIANCE MATRIX
// =============================================================================

export interface ComplianceMatrixEntry {
  role: string
  role_name: string
  regulations: Record<string, number>
  overall_score: number
  required_modules: number
  completed_modules: number
}

export interface GCIMatrixResponse {
  tenant_id: string
  matrix: ComplianceMatrixEntry[]
}

// =============================================================================
// NIS2
// =============================================================================

export interface NIS2Role {
  id: string
  name: string
  description: string
  mandatory_modules: string[]
  priority: number
}

export interface NIS2AreaScore {
  area_id: string
  area_name: string
  weight: number
  score: number
  weighted_score: number
}

export interface NIS2RoleScore {
  role_id: string
  role_name: string
  assigned_users: number
  completion_rate: number
  modules_completed: number
  modules_required: number
}

export interface NIS2Score {
  tenant_id: string
  overall_score: number
  maturity_level: string
  areas: NIS2AreaScore[]
  role_scores: NIS2RoleScore[]
  calculated_at: string
}

// =============================================================================
// ISO 27001
// =============================================================================

export interface ISOControl {
  id: string
  name: string
  description: string
  category_id: string
  category_name: string
  control_type: string
  is_critical: boolean
  sdk_modules: string[]
}

export interface ISOGap {
  control_id: string
  control_name: string
  category: string
  status: string
  priority: string
  recommendation: string
}

export interface ISOCategorySummary {
  category_id: string
  category_name: string
  total_controls: number
  covered_full: number
  covered_partial: number
  not_covered: number
}

export interface ISOGapAnalysis {
  tenant_id: string
  total_controls: number
  covered_full: number
  covered_partial: number
  not_covered: number
  coverage_percent: number
  category_summaries: ISOCategorySummary[]
  gaps: ISOGap[]
  calculated_at: string
}

// =============================================================================
// WEIGHT PROFILES
// =============================================================================

export interface WeightProfile {
  id: string
  name: string
  description: string
  weights: Record<string, number>
}

// =============================================================================
// HELPERS
// =============================================================================

export function getScoreColor(score: number): string {
  if (score >= 80) return 'text-green-600'
  if (score >= 60) return 'text-yellow-600'
  if (score >= 40) return 'text-orange-600'
  return 'text-red-600'
}

export function getScoreBgColor(score: number): string {
  if (score >= 80) return 'bg-green-500'
  if (score >= 60) return 'bg-yellow-500'
  if (score >= 40) return 'bg-orange-500'
  return 'bg-red-500'
}

export function getScoreRingColor(score: number): string {
  if (score >= 80) return '#22c55e'
  if (score >= 60) return '#eab308'
  if (score >= 40) return '#f97316'
  return '#ef4444'
}
