/**
 * Document generator types (Legal Templates RAG), DSFA RAG types,
 * and Compliance Wiki types.
 */

import type { CompanyProfile } from './company-profile'

// =============================================================================
// DOCUMENT GENERATOR (Legal Templates RAG)
// =============================================================================

export type LicenseType =
  | 'public_domain'
  | 'cc0'
  | 'unlicense'
  | 'mit'
  | 'cc_by_4'
  | 'reuse_notice'

export type TemplateType =
  // Legal / Vertragsvorlagen
  | 'privacy_policy'
  | 'terms_of_service'
  | 'agb'
  | 'cookie_banner'
  | 'cookie_policy'
  | 'impressum'
  | 'widerruf'
  | 'dpa'
  | 'sla'
  | 'nda'
  | 'cloud_service_agreement'
  | 'data_usage_clause'
  | 'acceptable_use'
  | 'community_guidelines'
  | 'copyright_policy'
  | 'clause'
  | 'dsfa'
  // Sicherheitskonzepte (Migration 051)
  | 'it_security_concept'
  | 'data_protection_concept'
  | 'backup_recovery_concept'
  | 'logging_concept'
  | 'incident_response_plan'
  | 'access_control_concept'
  | 'risk_management_concept'
  // CRA Cybersecurity (Migration 056)
  | 'cybersecurity_policy'
  // IT-Sicherheit Policies (Migration 071)
  | 'information_security_policy'
  | 'access_control_policy'
  | 'password_policy'
  | 'encryption_policy'
  | 'logging_policy'
  | 'backup_policy'
  | 'incident_response_policy'
  | 'change_management_policy'
  | 'patch_management_policy'
  | 'asset_management_policy'
  | 'cloud_security_policy'
  | 'devsecops_policy'
  | 'secrets_management_policy'
  | 'vulnerability_management_policy'
  // Daten-Policies (Migration 072)
  | 'data_protection_policy'
  | 'data_classification_policy'
  | 'data_retention_policy'
  | 'data_transfer_policy'
  | 'privacy_incident_policy'
  // Personal-Policies (Migration 072)
  | 'employee_security_policy'
  | 'security_awareness_policy'
  | 'remote_work_policy'
  | 'offboarding_policy'
  // Lieferanten-Policies (Migration 072)
  | 'vendor_risk_management_policy'
  | 'third_party_security_policy'
  | 'supplier_security_policy'
  // BCM/Notfall (Migration 072)
  | 'business_continuity_policy'
  | 'disaster_recovery_policy'
  | 'crisis_management_policy'
  // Modul-Dokumente (Migration 073)
  | 'vvt_register'
  | 'tom_documentation'
  | 'loeschkonzept'
  | 'pflichtenregister'
  // SOP (Migration 112)
  | 'standard_operating_procedure'

export type Jurisdiction = 'DE' | 'AT' | 'CH' | 'EU' | 'US' | 'INTL'

export interface LegalTemplateResult {
  source?: string
  id: string
  score: number
  text: string
  documentTitle: string | null
  templateType: TemplateType | null
  clauseCategory: string | null
  language: 'de' | 'en'
  jurisdiction: Jurisdiction | null
  licenseId: LicenseType | null
  licenseName: string | null
  licenseUrl: string | null
  attributionRequired: boolean
  attributionText: string | null
  sourceName: string | null
  sourceUrl: string | null
  sourceRepo: string | null
  placeholders: string[]
  isCompleteDocument: boolean
  isModular: boolean
  requiresCustomization: boolean
  outputAllowed: boolean
  modificationAllowed: boolean
  distortionProhibited: boolean
}

export interface TemplateReference {
  templateId: string
  sourceName: string
  sourceUrl: string
  licenseId: LicenseType
  licenseName: string
  attributionRequired: boolean
  attributionText: string | null
  usedAt: string
}

export interface GeneratedDocument {
  id: string
  documentType: TemplateType
  title: string
  content: string
  language: 'de' | 'en'
  jurisdiction: Jurisdiction
  usedTemplates: TemplateReference[]
  attributionFooter: string
  placeholderValues: Record<string, string>
  customizations: DocumentCustomization[]
  generatedAt: string
  generatedBy: string
  version: number
}

export interface DocumentCustomization {
  type: 'add_section' | 'modify_section' | 'remove_section' | 'replace_placeholder'
  section: string | null
  originalText: string | null
  newText: string | null
  reason: string | null
  appliedAt: string
}

export interface DocumentGeneratorState {
  searchQuery: string
  searchResults: LegalTemplateResult[]
  selectedTemplates: string[]
  currentDocumentType: TemplateType | null
  currentLanguage: 'de' | 'en'
  currentJurisdiction: Jurisdiction
  editorContent: string
  editorMode: 'preview' | 'edit'
  unsavedChanges: boolean
  placeholderValues: Record<string, string>
  generatedDocuments: GeneratedDocument[]
  isGenerating: boolean
  isSearching: boolean
  lastError: string | null
}

export interface TemplateSearchRequest {
  query: string
  templateType?: TemplateType
  licenseTypes?: LicenseType[]
  language?: 'de' | 'en'
  jurisdiction?: Jurisdiction
  attributionRequired?: boolean
  limit?: number
}

export interface DocumentGenerationRequest {
  documentType: TemplateType
  language: 'de' | 'en'
  jurisdiction: Jurisdiction
  templateIds: string[]
  placeholderValues: Record<string, string>
  companyProfile?: Partial<CompanyProfile>
  additionalContext?: string
}

export interface TemplateSource {
  name: string
  description: string
  licenseType: LicenseType
  licenseName: string
  templateTypes: TemplateType[]
  languages: ('de' | 'en')[]
  jurisdiction: Jurisdiction
  repoUrl: string | null
  webUrl: string | null
  priority: number
  enabled: boolean
  attributionRequired: boolean
}

export interface TemplateIngestionStatus {
  running: boolean
  lastRun: string | null
  currentSource: string | null
  results: Record<string, SourceIngestionResult>
}

export interface SourceIngestionResult {
  status: 'pending' | 'running' | 'completed' | 'failed'
  documentsFound: number
  chunksIndexed: number
  errors: string[]
}

export interface TemplateCollectionStats {
  collection: string
  vectorsCount: number
  pointsCount: number
  status: string
  templateTypes: Record<TemplateType, number>
  languages: Record<string, number>
  licenses: Record<LicenseType, number>
}

// =============================================================================
// LABEL CONSTANTS
// =============================================================================

export const DEFAULT_PLACEHOLDERS: Record<string, string> = {
  '[COMPANY_NAME]': '',
  '[FIRMENNAME]': '',
  '[ADDRESS]': '',
  '[ADRESSE]': '',
  '[EMAIL]': '',
  '[PHONE]': '',
  '[TELEFON]': '',
  '[WEBSITE]': '',
  '[LEGAL_REPRESENTATIVE]': '',
  '[GESCHAEFTSFUEHRER]': '',
  '[REGISTER_COURT]': '',
  '[REGISTERGERICHT]': '',
  '[REGISTER_NUMBER]': '',
  '[REGISTERNUMMER]': '',
  '[VAT_ID]': '',
  '[UST_ID]': '',
  '[DPO_NAME]': '',
  '[DSB_NAME]': '',
  '[DPO_EMAIL]': '',
  '[DSB_EMAIL]': '',
}

export const TEMPLATE_TYPE_LABELS: Record<TemplateType, string> = {
  // Legal / Vertragsvorlagen
  standard_operating_procedure: 'Standard Operating Procedure (SOP)',
  privacy_policy: 'Datenschutzerkl\u00e4rung',
  terms_of_service: 'Nutzungsbedingungen',
  agb: 'Allgemeine Gesch\u00e4ftsbedingungen',
  cookie_banner: 'Cookie-Banner',
  cookie_policy: 'Cookie-Richtlinie',
  impressum: 'Impressum',
  widerruf: 'Widerrufsbelehrung',
  dpa: 'Auftragsverarbeitungsvertrag',
  sla: 'Service Level Agreement',
  nda: 'Geheimhaltungsvereinbarung',
  cloud_service_agreement: 'Cloud-Dienstleistungsvertrag',
  data_usage_clause: 'Datennutzungsklausel',
  acceptable_use: 'Acceptable Use Policy',
  community_guidelines: 'Community-Richtlinien',
  copyright_policy: 'Urheberrechtsrichtlinie',
  clause: 'Vertragsklausel',
  dsfa: 'Datenschutz-Folgenabsch\u00e4tzung',
  // Sicherheitskonzepte
  it_security_concept: 'IT-Sicherheitskonzept',
  data_protection_concept: 'Datenschutzkonzept',
  backup_recovery_concept: 'Backup- und Recovery-Konzept',
  logging_concept: 'Logging-Konzept',
  incident_response_plan: 'Incident-Response-Plan',
  access_control_concept: 'Zugriffskonzept',
  risk_management_concept: 'Risikomanagement-Konzept',
  cybersecurity_policy: 'Cybersecurity-Richtlinie (CRA)',
  // IT-Sicherheit Policies
  information_security_policy: 'Informationssicherheitsrichtlinie',
  access_control_policy: 'Zugriffskontrollrichtlinie',
  password_policy: 'Passwortrichtlinie',
  encryption_policy: 'Verschl\u00fcsselungsrichtlinie',
  logging_policy: 'Protokollierungsrichtlinie',
  backup_policy: 'Datensicherungsrichtlinie',
  incident_response_policy: 'Incident-Response-Richtlinie',
  change_management_policy: 'Change-Management-Richtlinie',
  patch_management_policy: 'Patch-Management-Richtlinie',
  asset_management_policy: 'Asset-Management-Richtlinie',
  cloud_security_policy: 'Cloud-Security-Richtlinie',
  devsecops_policy: 'DevSecOps-Richtlinie',
  secrets_management_policy: 'Secrets-Management-Richtlinie',
  vulnerability_management_policy: 'Schwachstellenmanagement-Richtlinie',
  // Daten-Policies
  data_protection_policy: 'Datenschutzrichtlinie',
  data_classification_policy: 'Datenklassifizierungsrichtlinie',
  data_retention_policy: 'Aufbewahrungsrichtlinie',
  data_transfer_policy: 'Daten\u00fcbermittlungsrichtlinie',
  privacy_incident_policy: 'Datenschutzvorfall-Richtlinie',
  // Personal-Policies
  employee_security_policy: 'Mitarbeiter-Sicherheitsrichtlinie',
  security_awareness_policy: 'Security-Awareness-Richtlinie',
  remote_work_policy: 'Remote-Work-Richtlinie',
  offboarding_policy: 'Offboarding-Richtlinie',
  // Lieferanten-Policies
  vendor_risk_management_policy: 'Lieferanten-Risikomanagement',
  third_party_security_policy: 'Drittanbieter-Sicherheitsrichtlinie',
  supplier_security_policy: 'Lieferanten-Sicherheitsanforderungen',
  // BCM/Notfall
  business_continuity_policy: 'Business-Continuity-Richtlinie',
  disaster_recovery_policy: 'Disaster-Recovery-Richtlinie',
  crisis_management_policy: 'Krisenmanagement-Richtlinie',
  // Modul-Dokumente
  vvt_register: 'Verarbeitungsverzeichnis (Art. 30)',
  tom_documentation: 'TOM-Dokumentation (Art. 32)',
  loeschkonzept: 'L\u00f6schkonzept (Art. 5/17)',
  pflichtenregister: 'Pflichtenregister',
}

export const LICENSE_TYPE_LABELS: Record<LicenseType, string> = {
  public_domain: 'Public Domain (\u00a75 UrhG)',
  cc0: 'CC0 1.0 Universal',
  unlicense: 'Unlicense',
  mit: 'MIT License',
  cc_by_4: 'CC BY 4.0 International',
  reuse_notice: 'EU Reuse Notice',
}

export const JURISDICTION_LABELS: Record<Jurisdiction, string> = {
  DE: 'Deutschland',
  AT: '\u00d6sterreich',
  CH: 'Schweiz',
  EU: 'Europ\u00e4ische Union',
  US: 'United States',
  INTL: 'International',
}

