/**
 * Checkpoint system, use case assessment, and screening types.
 */

import type {
  ValidationSeverity,
  CheckpointType,
  ReviewerType,
  RiskSeverity,
  SecurityIssueSeverity,
  SecurityIssueStatus,
  ScreeningStatus,
  SDKPackageId,
} from './enums'

// =============================================================================
// CHECKPOINT SYSTEM
// =============================================================================

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
}

// =============================================================================
// USE CASE ASSESSMENT
// =============================================================================

export interface UseCaseStep {
  id: string
  name: string
  completed: boolean
  data: Record<string, unknown>
}

export interface AssessmentResult {
  riskLevel: RiskSeverity
  applicableRegulations: string[]
  recommendedControls: string[]
  dsfaRequired: boolean
  aiActClassification: string
}

export interface UseCaseIntake {
  domain: string
  dataCategories: string[]
  processesPersonalData: boolean
  specialCategories: boolean
  healthData: boolean
  biometricData: boolean
  minorsData: boolean
  financialData: boolean
  customDataTypes: string[]
  legalBasis: string
  purposes: {
    profiling: boolean
    automatedDecision: boolean
    marketing: boolean
    analytics: boolean
    serviceDelivery: boolean
  }
  automation: 'assistive' | 'semi_automated' | 'fully_automated'
  hosting: {
    provider: string
    region: string
  }
  modelUsage: {
    inference: boolean
    rag: boolean
    finetune: boolean
    training: boolean
  }
  aiTechnologies: string[]
  internationalTransfer: {
    enabled: boolean
    countries: string[]
    mechanism: string
  }
  retention: {
    days: number
    purpose: string
  }
  contracts: {
    hasDpa: boolean
    hasAiaDocumentation: boolean
    hasRiskAssessment: boolean
    subprocessors: string
  }
}

export interface UseCaseAssessment {
  id: string
  name: string
  description: string
  category: string
  stepsCompleted: number
  steps: UseCaseStep[]
  assessmentResult: AssessmentResult | null
  intake?: UseCaseIntake
  uccaAssessmentId?: string
  createdAt: Date
  updatedAt: Date
}

// =============================================================================
// SCREENING & SECURITY
// =============================================================================

export interface Vulnerability {
  id: string
  cve: string
  severity: SecurityIssueSeverity
  title: string
  description: string
  cvss: number | null
  fixedIn: string | null
}

export interface SBOMComponent {
  name: string
  version: string
  type: 'library' | 'framework' | 'application' | 'container'
  purl: string
  licenses: string[]
  vulnerabilities: Vulnerability[]
}

export interface SBOMDependency {
  from: string
  to: string
}

export interface RAGCorpusCollectionStatus {
  id: string
  current_version: string
  documents_count: number
  chunks_count: number
  regulations: string[]
  last_updated: string
  digest: string
}

export interface RAGCorpusStatus {
  collections: Record<string, RAGCorpusCollectionStatus>
  fetchedAt: string
}

export interface SBOM {
  format: 'CycloneDX' | 'SPDX'
  version: string
  components: SBOMComponent[]
  dependencies: SBOMDependency[]
  generatedAt: Date
}

export interface SecurityScanResult {
  totalIssues: number
  critical: number
  high: number
  medium: number
  low: number
  issues: SecurityIssue[]
}

export interface SecurityIssue {
  id: string
  severity: SecurityIssueSeverity
  title: string
  description: string
  cve: string | null
  cvss: number | null
  affectedComponent: string
  remediation: string
  status: SecurityIssueStatus
}

export interface ScreeningResult {
  id: string
  status: ScreeningStatus
  startedAt: Date
  completedAt: Date | null
  sbom: SBOM | null
  securityScan: SecurityScanResult | null
  error: string | null
}

export interface BacklogItem {
  id: string
  title: string
  description: string
  severity: SecurityIssueSeverity
  securityIssueId: string
  status: 'OPEN' | 'IN_PROGRESS' | 'DONE'
  assignee: string | null
  dueDate: Date | null
  createdAt: Date
}

// =============================================================================
// 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[]
}
