/**
 * DSGVO Module Types
 *
 * Type definitions for GDPR/DSGVO compliance features:
 * - DSR (Data Subject Requests) - Art. 15-21
 * - Consent Management - Art. 6, 7
 * - VVT (Processing Register) - Art. 30
 * - DSFA (Impact Assessment) - Art. 35, 36
 * - TOM (Technical & Organizational Measures) - Art. 32
 * - Retention Policies - Art. 5, 17
 */

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

// =============================================================================
// DSR (Data Subject Requests) - Art. 15-21 DSGVO
// =============================================================================

export type DSRRequestType =
  | 'ACCESS'       // Art. 15 - Auskunftsrecht
  | 'RECTIFICATION' // Art. 16 - Berichtigungsrecht
  | 'ERASURE'      // Art. 17 - Löschungsrecht
  | 'PORTABILITY'  // Art. 20 - Datenübertragbarkeit
  | 'RESTRICTION'  // Art. 18 - Einschränkungsrecht
  | 'OBJECTION'    // Art. 21 - Widerspruchsrecht

export type DSRStatus =
  | 'RECEIVED'
  | 'VERIFIED'
  | 'PROCESSING'
  | 'COMPLETED'
  | 'REJECTED'

export interface DSRRequest {
  id: string
  type: DSRRequestType
  status: DSRStatus
  requesterEmail: string
  requesterName: string
  requestedAt: Date
  dueDate: Date
  completedAt: Date | null
  notes: string
  verificationMethod?: string
  responseDocument?: string
}

export interface DSRConfig {
  id: string
  enabled: boolean
  portalUrl: string
  emailTemplates: Record<DSRRequestType, string>
  automatedResponses: boolean
  verificationRequired: boolean
  deadlineDays: number // Default 30 days per GDPR
}

// =============================================================================
// CONSENT MANAGEMENT - Art. 6, 7 DSGVO
// =============================================================================

export type ConsentPurpose =
  | 'ESSENTIAL'
  | 'FUNCTIONAL'
  | 'ANALYTICS'
  | 'MARKETING'
  | 'PERSONALIZATION'
  | 'THIRD_PARTY'

export type ConsentChannel =
  | 'WEB'
  | 'MOBILE_APP'
  | 'EMAIL'
  | 'PAPER'
  | 'VERBAL'
  | 'API'

export interface ConsentRecord {
  id: string
  userId: string
  documentId: string
  documentVersion: string
  consentType: ConsentPurpose
  channel: ConsentChannel
  granted: boolean
  grantedAt: Date
  revokedAt: Date | null
  ipAddress: string | null
  userAgent: string | null
  metadata?: Record<string, unknown>
}

export interface ConsentPreference {
  purpose: ConsentPurpose
  granted: boolean
  lastUpdated: Date
}

export interface ConsentState {
  userId: string
  preferences: ConsentPreference[]
  lastConsentDate: Date
  consentVersion: string
}

// =============================================================================
// COOKIE BANNER - Art. 7, 13/14 DSGVO
// =============================================================================

export type CookieBannerStyle = 'BANNER' | 'MODAL' | 'FLOATING'

export type CookieBannerPosition = 'TOP' | 'BOTTOM' | 'CENTER'

export type CookieBannerTheme = 'LIGHT' | 'DARK' | 'CUSTOM'

export type CookieType = 'NECESSARY' | 'FUNCTIONAL' | 'ANALYTICS' | 'MARKETING'

export interface Cookie {
  id: string
  name: string
  provider: string
  purpose: string
  expiry: string
  type: CookieType
}

export interface CookieCategory {
  id: string
  name: string
  description: string
  required: boolean
  cookies: Cookie[]
}

export interface CookieBannerTexts {
  title: string
  description: string
  acceptAll: string
  rejectAll: string
  settings: string
  save: string
  necessary: string
  functional: string
  analytics: string
  marketing: string
}

export interface CookieBannerGeneratedCode {
  html: string
  css: string
  js: string
}

export interface CookieBannerConfig {
  id: string
  style: CookieBannerStyle
  position: CookieBannerPosition
  theme: CookieBannerTheme
  customColors?: {
    primary: string
    secondary: string
    background: string
    text: string
  }
  texts: CookieBannerTexts
  categories: CookieCategory[]
  generatedCode: CookieBannerGeneratedCode | null
  logoUrl?: string
  privacyPolicyUrl: string
  imprintUrl: string
}

// =============================================================================
// VVT (Verarbeitungsverzeichnis) - Art. 30 DSGVO
// =============================================================================

export type LegalBasis =
  | 'CONSENT'           // Art. 6(1)(a) - Einwilligung
  | 'CONTRACT'          // Art. 6(1)(b) - Vertragserfüllung
  | 'LEGAL_OBLIGATION'  // Art. 6(1)(c) - Rechtliche Verpflichtung
  | 'VITAL_INTERESTS'   // Art. 6(1)(d) - Lebenswichtige Interessen
  | 'PUBLIC_INTEREST'   // Art. 6(1)(e) - Öffentliches Interesse
  | 'LEGITIMATE_INTEREST' // Art. 6(1)(f) - Berechtigtes Interesse

export interface ProcessingActivity {
  id: string
  name: string
  purpose: string
  legalBasis: LegalBasis
  legalBasisJustification?: string
  dataCategories: string[]
  specialDataCategories?: string[] // Art. 9 Daten
  dataSubjects: string[]
  recipients: string[]
  thirdCountryTransfers: boolean
  transferSafeguards?: string // SCCs, BCRs, etc.
  retentionPeriod: string
  technicalMeasures: string[]
  organizationalMeasures: string[]
  responsiblePerson: string
  lastReviewDate: Date
  nextReviewDate: Date
}

// =============================================================================
// DSFA (Datenschutz-Folgenabschätzung) - Art. 35, 36 DSGVO
// =============================================================================

export type DSFAStatus = 'DRAFT' | 'IN_REVIEW' | 'APPROVED' | 'REJECTED'

export interface DSFASection {
  id: string
  title: string
  content: string
  status: 'DRAFT' | 'COMPLETED'
  order: number
}

export interface DSFAApproval {
  id: string
  approver: string
  role: string
  status: 'PENDING' | 'APPROVED' | 'REJECTED'
  comment: string | null
  approvedAt: Date | null
}

export interface DSFARisk {
  id: string
  description: string
  likelihood: number
  severity: number
  riskScore: number
  mitigation: string
  residualRisk: number
}

export interface DSFA {
  id: string
  name: string
  processingActivityId: string
  status: DSFAStatus
  version: number
  sections: DSFASection[]
  risks: DSFARisk[]
  approvals: DSFAApproval[]
  consultationRequired: boolean // Art. 36 consultation
  consultationDate?: Date
  createdAt: Date
  updatedAt: Date
}

// =============================================================================
// TOM (Technische und Organisatorische Maßnahmen) - Art. 32 DSGVO
// =============================================================================

export type TOMType = 'TECHNICAL' | 'ORGANIZATIONAL'

export type TOMCategory =
  | 'ACCESS_CONTROL'        // Zutrittskontrolle
  | 'DATA_ACCESS_CONTROL'   // Zugangskontrolle
  | 'USER_ACCESS_CONTROL'   // Zugriffskontrolle
  | 'TRANSFER_CONTROL'      // Weitergabekontrolle
  | 'INPUT_CONTROL'         // Eingabekontrolle
  | 'JOB_CONTROL'           // Auftragskontrolle
  | 'AVAILABILITY_CONTROL'  // Verfügbarkeitskontrolle
  | 'SEPARATION_CONTROL'    // Trennungskontrolle

export interface TOM {
  id: string
  category: TOMCategory
  name: string
  description: string
  type: TOMType
  implementationStatus: ImplementationStatus
  priority: RiskSeverity
  responsiblePerson: string | null
  implementationDate: Date | null
  reviewDate: Date | null
  evidence: string[]
  controls: string[] // Related control IDs
}

export interface TOMGeneratorConfig {
  companySize: 'SMALL' | 'MEDIUM' | 'LARGE' | 'ENTERPRISE'
  industry: string
  dataCategories: string[]
  riskLevel: RiskSeverity
  existingMeasures: string[]
}

export interface TOMGeneratorResult {
  recommendedTOMs: TOM[]
  score: number // 0-100%
  gaps: string[]
  recommendations: string[]
}

// =============================================================================
// RETENTION POLICIES - Art. 5, 17 DSGVO
// =============================================================================

export interface RetentionPolicy {
  id: string
  dataCategory: string
  description: string
  legalBasis: string
  retentionPeriod: string // ISO 8601 Duration (e.g., "P3Y" for 3 years)
  retentionPeriodDays: number
  deletionMethod: 'ANONYMIZATION' | 'PSEUDONYMIZATION' | 'HARD_DELETE' | 'SOFT_DELETE'
  exceptions: string[]
  automatedDeletion: boolean
  lastReviewDate: Date
  nextReviewDate: Date
}

export interface RetentionSchedule {
  id: string
  policyId: string
  nextExecutionDate: Date
  lastExecutionDate: Date | null
  status: 'SCHEDULED' | 'RUNNING' | 'COMPLETED' | 'FAILED'
  itemsProcessed?: number
  errors?: string[]
}

// =============================================================================
// ESCALATIONS - Art. 33/34 DSGVO (Data Breach)
// =============================================================================

export type EscalationLevel = 'E0' | 'E1' | 'E2' | 'E3'

export interface EscalationStep {
  id: string
  order: number
  action: string
  assignee: string
  timeLimit: string // ISO 8601 Duration
  escalateOnTimeout: boolean
  notificationChannels: ('EMAIL' | 'SMS' | 'SLACK' | 'WEBHOOK')[]
}

export interface EscalationWorkflow {
  id: string
  name: string
  description: string
  level: EscalationLevel
  triggerConditions: string[]
  steps: EscalationStep[]
  enabled: boolean
  createdAt: Date
  updatedAt: Date
}

export interface DataBreach {
  id: string
  title: string
  description: string
  discoveredAt: Date
  reportedToAuthorityAt?: Date // Art. 33 - 72 hours
  reportedToDataSubjectsAt?: Date // Art. 34
  affectedDataSubjects: number
  dataCategories: string[]
  riskLevel: RiskSeverity
  mitigationMeasures: string[]
  status: 'DETECTED' | 'INVESTIGATING' | 'REPORTED' | 'RESOLVED'
  workflowId: string
}

// =============================================================================
// LEGAL DOCUMENTS
// =============================================================================

export type LegalDocumentType =
  | 'AGB'
  | 'PRIVACY_POLICY'
  | 'TERMS_OF_USE'
  | 'IMPRINT'
  | 'COOKIE_POLICY'
  | 'DATA_PROCESSING_AGREEMENT'

export interface LegalDocument {
  id: string
  type: LegalDocumentType
  title: string
  content: string
  version: string
  status: 'DRAFT' | 'PUBLISHED' | 'ARCHIVED'
  language: 'de' | 'en'
  publishedAt: Date | null
  createdAt: Date
  updatedAt: Date
  changelog?: string
}
