/**
 * Documentation & Legal types
 *
 * TOMs, retention policies, VVT processing activities,
 * legal documents, cookie banner, consent/DSR,
 * imported documents, gap analysis, and escalation workflows.
 */

import type {
  RiskSeverity,
  ImplementationStatus,
  CookieBannerStyle,
  CookieBannerPosition,
  CookieBannerTheme,
  SDKPackageId,
} from './core'

// =============================================================================
// TOMs & RETENTION
// =============================================================================

export interface TOM {
  id: string
  category: string
  name: string
  description: string
  type: 'TECHNICAL' | 'ORGANIZATIONAL'
  implementationStatus: ImplementationStatus
  priority: RiskSeverity
  responsiblePerson: string | null
  implementationDate: Date | null
  reviewDate: Date | null
  evidence: string[]
}

export interface RetentionPolicy {
  id: string
  dataCategory: string
  description: string
  legalBasis: string
  retentionPeriod: string
  deletionMethod: string
  exceptions: string[]
}

// =============================================================================
// VVT (Processing Register)
// =============================================================================

export interface ProcessingActivity {
  id: string
  name: string
  purpose: string
  legalBasis: string
  dataCategories: string[]
  dataSubjects: string[]
  recipients: string[]
  thirdCountryTransfers: boolean
  retentionPeriod: string
  technicalMeasures: string[]
  organizationalMeasures: string[]
}

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

export interface LegalDocument {
  id: string
  type: 'AGB' | 'PRIVACY_POLICY' | 'TERMS_OF_USE' | 'IMPRINT' | 'COOKIE_POLICY'
  title: string
  content: string
  version: string
  status: 'DRAFT' | 'PUBLISHED' | 'ARCHIVED'
  publishedAt: Date | null
  createdAt: Date
  updatedAt: Date
}

// =============================================================================
// COOKIE BANNER
// =============================================================================

export interface Cookie {
  id: string
  name: string
  provider: string
  purpose: string
  expiry: string
  type: 'NECESSARY' | 'FUNCTIONAL' | 'ANALYTICS' | 'MARKETING'
}

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
}

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

export interface CookieBannerConfig {
  id: string
  style: CookieBannerStyle
  position: CookieBannerPosition
  theme: CookieBannerTheme
  texts: CookieBannerTexts
  categories: CookieCategory[]
  generatedCode: CookieBannerGeneratedCode | null
}

// =============================================================================
// CONSENT & DSR
// =============================================================================

export interface ConsentRecord {
  id: string
  userId: string
  documentId: string
  documentVersion: string
  consentType: string
  granted: boolean
  grantedAt: Date
  revokedAt: Date | null
  ipAddress: string | null
  userAgent: string | null
}

export interface DSRRequest {
  id: string
  type: 'ACCESS' | 'RECTIFICATION' | 'ERASURE' | 'PORTABILITY' | 'RESTRICTION' | 'OBJECTION'
  status: 'RECEIVED' | 'VERIFIED' | 'PROCESSING' | 'COMPLETED' | 'REJECTED'
  requesterEmail: string
  requesterName: string
  requestedAt: Date
  dueDate: Date
  completedAt: Date | null
  notes: string
}

export interface DSRConfig {
  id: string
  enabled: boolean
  portalUrl: string
  emailTemplates: Record<string, string>
  automatedResponses: boolean
  verificationRequired: boolean
}

// =============================================================================
// IMPORTED DOCUMENTS (fuer Bestandskunden)
// =============================================================================

export type ImportedDocumentType =
  | 'DSFA'
  | 'TOM'
  | 'VVT'
  | 'AGB'
  | 'PRIVACY_POLICY'
  | 'COOKIE_POLICY'
  | 'RISK_ASSESSMENT'
  | 'AUDIT_REPORT'
  | 'OTHER'

export interface ImportedDocument {
  id: string
  name: string
  type: ImportedDocumentType
  fileUrl: string
  uploadedAt: Date
  analyzedAt: Date | null
  analysisResult: DocumentAnalysisResult | null
}

export interface DocumentAnalysisResult {
  detectedType: ImportedDocumentType
  confidence: number
  extractedEntities: string[]
  gaps: GapItem[]
  recommendations: string[]
}

export interface GapItem {
  id: string
  category: string
  description: string
  severity: RiskSeverity
  regulation: string
  requiredAction: string
  relatedStepId: string | null
}

export interface GapAnalysis {
  id: string
  createdAt: Date
  totalGaps: number
  criticalGaps: number
  highGaps: number
  mediumGaps: number
  lowGaps: number
  gaps: GapItem[]
  recommendedPackages: SDKPackageId[]
}

// =============================================================================
// ESCALATIONS
// =============================================================================

export interface EscalationWorkflow {
  id: string
  name: string
  description: string
  triggerConditions: string[]
  steps: EscalationStep[]
  enabled: boolean
}

export interface EscalationStep {
  id: string
  order: number
  action: string
  assignee: string
  timeLimit: string // ISO 8601 Duration
  escalateOnTimeout: boolean
}
