/**
 * Compliance Module Types
 *
 * Type definitions for general compliance features:
 * - Controls & Control Catalog
 * - Evidence Management
 * - Obligations & Requirements
 * - AI Act Classification
 * - NIS2 Compliance
 * - Audit & Assessment
 */

import type {
  RiskSeverity,
  ImplementationStatus,
  RequirementStatus,
  ValidationError,
} from './base'

// =============================================================================
// REGULATIONS
// =============================================================================

export type RegulationCode =
  | 'DSGVO'
  | 'AI_ACT'
  | 'NIS2'
  | 'EPRIVACY'
  | 'TDDDG'
  | 'SCC'
  | 'DPF'
  | 'CRA'
  | 'EUCSA'
  | 'DATA_ACT'
  | 'DGA'
  | 'DSA'
  | 'EAA'
  | 'ISO_27001'
  | 'SOC2'
  | 'BSI_GRUNDSCHUTZ'

export interface Regulation {
  id: string
  code: RegulationCode
  name: string
  fullName: string
  description: string
  jurisdiction: 'EU' | 'DE' | 'INT'
  effectiveDate: Date
  requirements: number
  controlMappings: number
  documentUrl?: string
}

// =============================================================================
// CONTROLS
// =============================================================================

export type ControlType = 'TECHNICAL' | 'ORGANIZATIONAL' | 'PHYSICAL'

export type ControlDomain =
  | 'ACCESS_MANAGEMENT'
  | 'DATA_PROTECTION'
  | 'NETWORK_SECURITY'
  | 'INCIDENT_RESPONSE'
  | 'BUSINESS_CONTINUITY'
  | 'VENDOR_MANAGEMENT'
  | 'ASSET_MANAGEMENT'
  | 'CRYPTOGRAPHY'
  | 'PHYSICAL_SECURITY'

export interface Control {
  id: string
  code: string // e.g., "AC-01", "DP-03"
  name: string
  description: string
  type: ControlType
  domain: ControlDomain
  implementationStatus: ImplementationStatus
  effectiveness: RiskSeverity
  evidence: string[]
  owner: string | null
  dueDate: Date | null
  mappings: ControlMapping[]
  testingFrequency: 'ANNUAL' | 'SEMI_ANNUAL' | 'QUARTERLY' | 'MONTHLY'
  lastTestDate?: Date
  nextTestDate?: Date
}

export interface ControlMapping {
  regulationCode: RegulationCode
  requirementId: string
  article: string
}

export interface ControlCatalog {
  id: string
  name: string
  version: string
  controls: Control[]
  domains: ControlDomain[]
  lastUpdated: Date
}

// =============================================================================
// REQUIREMENTS
// =============================================================================

export interface Requirement {
  id: string
  regulationCode: RegulationCode
  article: string
  title: string
  description: string
  criticality: RiskSeverity
  applicableModules: string[]
  status: RequirementStatus
  controls: string[] // Control IDs
  deadline?: Date
  notes?: string
}

// =============================================================================
// EVIDENCE
// =============================================================================

export type EvidenceType =
  | 'DOCUMENT'
  | 'SCREENSHOT'
  | 'LOG'
  | 'CERTIFICATE'
  | 'AUDIT_REPORT'
  | 'POLICY'
  | 'TRAINING_RECORD'

export type EvidenceStatus = 'DRAFT' | 'ACTIVE' | 'EXPIRED' | 'ARCHIVED'

export interface Evidence {
  id: string
  controlId: string
  type: EvidenceType
  name: string
  description: string
  fileUrl: string | null
  mimeType?: string
  fileSize?: number
  validFrom: Date
  validUntil: Date | null
  status: EvidenceStatus
  uploadedBy: string
  uploadedAt: Date
  reviewedBy?: string
  reviewedAt?: Date
  tags: string[]
}

export interface EvidenceUploadRequest {
  controlId: string
  type: EvidenceType
  name: string
  description?: string
  validFrom?: Date
  validUntil?: Date
  tags?: string[]
  file: File | Blob
}

// =============================================================================
// AI ACT
// =============================================================================

export type AIActRiskCategory =
  | 'MINIMAL'       // Minimal or no risk
  | 'LIMITED'       // Limited risk - transparency
  | 'HIGH'          // High-risk - conformity assessment
  | 'UNACCEPTABLE'  // Prohibited

export type AISystemType =
  | 'GENERAL_PURPOSE'
  | 'BIOMETRIC'
  | 'CRITICAL_INFRASTRUCTURE'
  | 'EDUCATION'
  | 'EMPLOYMENT'
  | 'ESSENTIAL_SERVICES'
  | 'LAW_ENFORCEMENT'
  | 'BORDER_CONTROL'
  | 'JUSTICE'
  | 'RECOMMENDATION'
  | 'CONTENT_GENERATION'

export interface AIActObligation {
  id: string
  article: string
  title: string
  description: string
  riskCategory: AIActRiskCategory
  deadline: Date | null
  status: 'PENDING' | 'IN_PROGRESS' | 'COMPLETED'
  evidence?: string[]
}

export interface AIActResult {
  riskCategory: AIActRiskCategory
  systemType: AISystemType
  obligations: AIActObligation[]
  assessmentDate: Date
  assessedBy: string
  justification: string
  mitigationRequired: boolean
  conformityAssessmentRequired: boolean
  transparencyRequired: boolean
}

export interface AIActAssessmentInput {
  systemDescription: string
  intendedUse: string
  dataCategories: string[]
  affectedPersons: string[]
  automatedDecisions: boolean
  humanOversight: boolean
  riskMitigations: string[]
}

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

export type NIS2Sector =
  | 'ENERGY'
  | 'TRANSPORT'
  | 'BANKING'
  | 'FINANCIAL_MARKET'
  | 'HEALTH'
  | 'DRINKING_WATER'
  | 'WASTE_WATER'
  | 'DIGITAL_INFRASTRUCTURE'
  | 'ICT_SERVICE_MANAGEMENT'
  | 'PUBLIC_ADMINISTRATION'
  | 'SPACE'
  | 'POSTAL'
  | 'WASTE_MANAGEMENT'
  | 'CHEMICAL'
  | 'FOOD'
  | 'MANUFACTURING'
  | 'DIGITAL_PROVIDERS'
  | 'RESEARCH'

export type NIS2EntityType = 'ESSENTIAL' | 'IMPORTANT'

export interface NIS2Assessment {
  sector: NIS2Sector
  entityType: NIS2EntityType
  employeeCount: number
  annualTurnover: number // in EUR
  applicableRequirements: NIS2Requirement[]
  complianceScore: number // 0-100
  gaps: string[]
}

export interface NIS2Requirement {
  id: string
  article: string
  title: string
  description: string
  controls: string[]
  status: RequirementStatus
  deadline?: Date
}

// =============================================================================
// OBLIGATIONS
// =============================================================================

export interface Obligation {
  id: string
  regulationCode: RegulationCode
  article: string
  title: string
  description: string
  deadline: Date | null
  penalty: string | null
  status: 'PENDING' | 'IN_PROGRESS' | 'COMPLETED'
  responsible: string | null
  priority: RiskSeverity
  linkedControls: string[]
  linkedEvidence: string[]
}

// =============================================================================
// AUDIT & ASSESSMENT
// =============================================================================

export type AuditStatus = 'PLANNED' | 'IN_PROGRESS' | 'COMPLETED' | 'CANCELLED'

export type AuditType = 'INTERNAL' | 'EXTERNAL' | 'CERTIFICATION' | 'REGULATORY'

export interface Audit {
  id: string
  name: string
  type: AuditType
  status: AuditStatus
  scope: string[]
  regulations: RegulationCode[]
  auditor: string
  startDate: Date
  endDate: Date | null
  findings: AuditFinding[]
  report?: string
}

export interface AuditFinding {
  id: string
  title: string
  description: string
  severity: RiskSeverity
  controlId?: string
  recommendation: string
  status: 'OPEN' | 'IN_PROGRESS' | 'RESOLVED' | 'ACCEPTED'
  dueDate?: Date
  responsiblePerson?: string
}

export interface ChecklistItem {
  id: string
  requirementId: string
  title: string
  description: string
  status: 'PENDING' | 'PASSED' | 'FAILED' | 'NOT_APPLICABLE'
  notes: string
  verifiedBy: string | null
  verifiedAt: Date | null
  evidence?: string[]
}

// =============================================================================
// COMPLIANCE SCORE
// =============================================================================

export interface ComplianceScore {
  overall: number // 0-100
  byRegulation: Record<RegulationCode, number>
  byDomain: Record<ControlDomain, number>
  trend: 'UP' | 'DOWN' | 'STABLE'
  lastCalculated: Date
}

export interface ComplianceGap {
  id: string
  regulationCode: RegulationCode
  requirementId: string
  controlId?: string
  description: string
  severity: RiskSeverity
  recommendation: string
  estimatedEffort: 'LOW' | 'MEDIUM' | 'HIGH'
}

// =============================================================================
// MODULES / SERVICE MODULES
// =============================================================================

export interface ServiceModule {
  id: string
  name: string
  description: string
  regulations: RegulationCode[]
  criticality: RiskSeverity
  processesPersonalData: boolean
  hasAIComponents: boolean
  dataCategories: string[]
  owner: string
}

// =============================================================================
// EXPORT
// =============================================================================

export interface ExportOptions {
  format: 'PDF' | 'DOCX' | 'JSON' | 'CSV' | 'ZIP'
  sections: ExportSection[]
  includeEvidence: boolean
  language: 'de' | 'en'
  template?: string
}

export type ExportSection =
  | 'SUMMARY'
  | 'CONTROLS'
  | 'EVIDENCE'
  | 'RISKS'
  | 'REQUIREMENTS'
  | 'OBLIGATIONS'
  | 'AUDIT_FINDINGS'
  | 'VVT'
  | 'DSFA'
  | 'TOM'
