/**
 * DSFA RAG types — source attribution and corpus management for the
 * Datenschutz-Folgenabschätzung RAG pipeline.
 */

export type DSFALicenseCode =
  | 'DL-DE-BY-2.0'
  | 'DL-DE-ZERO-2.0'
  | 'CC-BY-4.0'
  | 'EDPB-LICENSE'
  | 'PUBLIC_DOMAIN'
  | 'PROPRIETARY'

export type DSFADocumentType = 'guideline' | 'checklist' | 'regulation' | 'template'

export type DSFACategory =
  | 'threshold_analysis'
  | 'risk_assessment'
  | 'mitigation'
  | 'consultation'
  | 'documentation'
  | 'process'
  | 'criteria'

export interface DSFASource {
  id: string
  sourceCode: string
  name: string
  fullName?: string
  organization?: string
  sourceUrl?: string
  eurLexCelex?: string
  licenseCode: DSFALicenseCode
  licenseName: string
  licenseUrl?: string
  attributionRequired: boolean
  attributionText: string
  documentType?: DSFADocumentType
  language: string
}

export interface DSFADocument {
  id: string
  sourceId: string
  title: string
  description?: string
  fileName?: string
  fileType?: string
  fileSizeBytes?: number
  minioBucket: string
  minioPath?: string
  originalUrl?: string
  ocrProcessed: boolean
  textExtracted: boolean
  chunksGenerated: number
  lastIndexedAt?: string
  metadata: Record<string, unknown>
  createdAt: string
  updatedAt: string
}

export interface DSFAChunk {
  chunkId: string
  content: string
  sectionTitle?: string
  pageNumber?: number
  category?: DSFACategory
  documentId: string
  documentTitle?: string
  sourceId: string
  sourceCode: string
  sourceName: string
  attributionText: string
  licenseCode: DSFALicenseCode
  licenseName: string
  licenseUrl?: string
  attributionRequired: boolean
  sourceUrl?: string
  documentType?: DSFADocumentType
}

export interface DSFASearchResult {
  chunkId: string
  content: string
  score: number
  sourceCode: string
  sourceName: string
  attributionText: string
  licenseCode: DSFALicenseCode
  licenseName: string
  licenseUrl?: string
  attributionRequired: boolean
  sourceUrl?: string
  documentType?: DSFADocumentType
  category?: DSFACategory
  sectionTitle?: string
  pageNumber?: number
}

export interface DSFASearchResponse {
  query: string
  results: DSFASearchResult[]
  totalResults: number
  licensesUsed: string[]
  attributionNotice: string
}

export interface DSFASourceStats {
  sourceId: string
  sourceCode: string
  name: string
  organization?: string
  licenseCode: DSFALicenseCode
  documentType?: DSFADocumentType
  documentCount: number
  chunkCount: number
  lastIndexedAt?: string
}

export interface DSFACorpusStats {
  sources: DSFASourceStats[]
  totalSources: number
  totalDocuments: number
  totalChunks: number
  qdrantCollection: string
  qdrantPointsCount: number
  qdrantStatus: string
}

export interface DSFALicenseInfo {
  code: DSFALicenseCode
  name: string
  url?: string
  attributionRequired: boolean
  modificationAllowed: boolean
  commercialUse: boolean
}

export interface DSFAIngestRequest {
  documentUrl?: string
  documentText?: string
  title?: string
}

export interface DSFAIngestResponse {
  sourceCode: string
  documentId?: string
  chunksCreated: number
  message: string
}

export interface SourceAttributionProps {
  sources: Array<{
    sourceCode: string
    sourceName: string
    attributionText: string
    licenseCode: DSFALicenseCode
    sourceUrl?: string
    score?: number
  }>
  compact?: boolean
  showScores?: boolean
}

export const DSFA_LICENSE_LABELS: Record<DSFALicenseCode, string> = {
  'DL-DE-BY-2.0': 'Datenlizenz DE \u2013 Namensnennung 2.0',
  'DL-DE-ZERO-2.0': 'Datenlizenz DE \u2013 Zero 2.0',
  'CC-BY-4.0': 'CC BY 4.0 International',
  'EDPB-LICENSE': 'EDPB Document License',
  'PUBLIC_DOMAIN': 'Public Domain',
  'PROPRIETARY': 'Proprietary',
}

export const DSFA_DOCUMENT_TYPE_LABELS: Record<DSFADocumentType, string> = {
  guideline: 'Leitlinie',
  checklist: 'Pr\u00fcfliste',
  regulation: 'Verordnung',
  template: 'Vorlage',
}

export const DSFA_CATEGORY_LABELS: Record<DSFACategory, string> = {
  threshold_analysis: 'Schwellwertanalyse',
  risk_assessment: 'Risikobewertung',
  mitigation: 'Risikominderung',
  consultation: 'Beh\u00f6rdenkonsultation',
  documentation: 'Dokumentation',
  process: 'Prozessschritte',
  criteria: 'Kriterien',
}
