/**
 * DSFA RAG types (Source Attribution & Corpus Management)
 *
 * Types for the DSFA (Data Protection Impact Assessment) RAG pipeline:
 * source documents, chunks, search results, corpus statistics,
 * and ingestion management.
 */

// =============================================================================
// DSFA ENUMS
// =============================================================================

/**
 * License codes for DSFA source documents
 */
export type DSFALicenseCode =
  | 'DL-DE-BY-2.0'      // Datenlizenz Deutschland -- Namensnennung
  | 'DL-DE-ZERO-2.0'    // Datenlizenz Deutschland -- Zero
  | 'CC-BY-4.0'         // Creative Commons Attribution 4.0
  | 'EDPB-LICENSE'      // EDPB Document License
  | 'PUBLIC_DOMAIN'     // Public Domain
  | 'PROPRIETARY'       // Internal/Proprietary

/**
 * Document types in the DSFA corpus
 */
export type DSFADocumentType = 'guideline' | 'checklist' | 'regulation' | 'template'

/**
 * Category for DSFA chunks (for filtering)
 */
export type DSFACategory =
  | 'threshold_analysis'
  | 'risk_assessment'
  | 'mitigation'
  | 'consultation'
  | 'documentation'
  | 'process'
  | 'criteria'

// =============================================================================
// DSFA SOURCE & DOCUMENTS
// =============================================================================

/**
 * DSFA source registry entry
 */
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
}

/**
 * DSFA document entry
 */
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
}

// =============================================================================
// DSFA CHUNKS & SEARCH
// =============================================================================

/**
 * DSFA chunk with full attribution
 */
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
}

/**
 * DSFA search result with score and attribution
 */
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
}

/**
 * DSFA search response with aggregated attribution
 */
export interface DSFASearchResponse {
  query: string
  results: DSFASearchResult[]
  totalResults: number
  licensesUsed: string[]
  attributionNotice: string
}

// =============================================================================
// DSFA STATISTICS & INGESTION
// =============================================================================

/**
 * Source statistics for dashboard
 */
export interface DSFASourceStats {
  sourceId: string
  sourceCode: string
  name: string
  organization?: string
  licenseCode: DSFALicenseCode
  documentType?: DSFADocumentType
  documentCount: number
  chunkCount: number
  lastIndexedAt?: string
}

/**
 * Corpus statistics for dashboard
 */
export interface DSFACorpusStats {
  sources: DSFASourceStats[]
  totalSources: number
  totalDocuments: number
  totalChunks: number
  qdrantCollection: string
  qdrantPointsCount: number
  qdrantStatus: string
}

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

/**
 * Ingestion request for DSFA documents
 */
export interface DSFAIngestRequest {
  documentUrl?: string
  documentText?: string
  title?: string
}

/**
 * Ingestion response
 */
export interface DSFAIngestResponse {
  sourceCode: string
  documentId?: string
  chunksCreated: number
  message: string
}

/**
 * Props for SourceAttribution component
 */
export interface SourceAttributionProps {
  sources: Array<{
    sourceCode: string
    sourceName: string
    attributionText: string
    licenseCode: DSFALicenseCode
    sourceUrl?: string
    score?: number
  }>
  compact?: boolean
  showScores?: boolean
}

// =============================================================================
// DISPLAY LABELS
// =============================================================================

/**
 * License code display labels
 */
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',
}

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

/**
 * Category display labels
 */
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',
}
