// ==============================================
// Architecture Data for Wizard Framework
// ==============================================
// Zentrale Konfiguration der Systemarchitektur fuer
// Visualisierung in den Admin-Wizards

export interface ServiceNode {
  name: string
  port: number
  type: 'frontend' | 'api' | 'service' | 'database' | 'cache' | 'external'
  description?: string
  dependencies?: string[]
}

export interface ModuleArchitecture {
  displayName: string
  description: string
  primaryServices: string[]
  databases: string[]
  tables?: string[]
  externalTools?: string[]
  dataFlow: string[]
  dependencies: string[]
}

// ==============================================
// Service-Definitionen
// ==============================================

export const ARCHITECTURE_SERVICES: Record<string, ServiceNode> = {
  frontend: {
    name: 'Next.js Admin',
    port: 3000,
    type: 'frontend',
    description: 'React-basiertes Admin Panel',
  },
  backend: {
    name: 'Python Backend',
    port: 8000,
    type: 'api',
    description: 'FastAPI REST API',
    dependencies: ['postgres', 'valkey'],
  },
  'consent-service': {
    name: 'Go Consent Service',
    port: 8081,
    type: 'service',
    description: 'DSGVO-konforme Einwilligungsverwaltung',
    dependencies: ['postgres'],
  },
  postgres: {
    name: 'PostgreSQL',
    port: 5432,
    type: 'database',
    description: 'Relationale Datenbank',
  },
  valkey: {
    name: 'Valkey',
    port: 6379,
    type: 'cache',
    description: 'In-Memory Cache & Session Store (Redis-kompatibler Open Source Fork)',
  },
  matrix: {
    name: 'Matrix Synapse',
    port: 8008,
    type: 'service',
    description: 'Dezentraler Messenger Server',
  },
  jitsi: {
    name: 'Jitsi Meet',
    port: 8443,
    type: 'service',
    description: 'Video-Konferenz Server',
  },
  mailserver: {
    name: 'Mailserver',
    port: 993,
    type: 'external',
    description: 'IMAP/SMTP Mailserver',
  },
  'unity-bridge': {
    name: 'Unity AI Bridge',
    port: 8090,
    type: 'external',
    description: 'REST API im Unity Editor fuer externe Steuerung',
  },
}

// ==============================================
// Modul-spezifische Architektur
// ==============================================

export const MODULE_ARCHITECTURE: Record<string, ModuleArchitecture> = {
  middleware: {
    displayName: 'Middleware Stack',
    description: 'Security & Request Processing Layer',
    primaryServices: ['backend'],
    databases: ['postgres', 'valkey'],
    tables: ['middleware_config', 'middleware_events', 'rate_limit_ips'],
    dataFlow: ['Browser', 'Next.js', 'FastAPI Middleware', 'PostgreSQL'],
    dependencies: ['JWT Auth', 'Valkey Session'],
  },
  consent: {
    displayName: 'Consent Verwaltung',
    description: 'DSGVO-konforme Einwilligungsverwaltung',
    primaryServices: ['consent-service'],
    databases: ['postgres'],
    tables: ['consent_records', 'document_versions', 'consent_templates'],
    dataFlow: ['Browser', 'Next.js', 'FastAPI', 'Go Consent Service', 'PostgreSQL'],
    dependencies: ['JWT Auth', 'RBAC (data_protection_officer)'],
  },
  dsr: {
    displayName: 'Datenschutzanfragen (DSR)',
    description: 'DSGVO Art. 15-21 Anfragenbearbeitung',
    primaryServices: ['backend', 'consent-service'],
    databases: ['postgres'],
    tables: ['dsr_requests', 'dsr_exports', 'audit_log'],
    dataFlow: ['Browser', 'Next.js', 'FastAPI', 'Go Consent Service', 'PostgreSQL'],
    dependencies: ['JWT Auth', 'RBAC', 'Export Service'],
  },
  security: {
    displayName: 'Security & DevSecOps',
    description: 'SAST, SCA, Secret Detection, SBOM',
    primaryServices: ['backend'],
    databases: ['postgres'],
    tables: ['security_scans', 'security_findings', 'sbom_components'],
    externalTools: ['gitleaks', 'semgrep', 'trivy', 'grype'],
    dataFlow: ['Git Repository', 'Scanner Tools', 'FastAPI', 'PostgreSQL'],
    dependencies: ['Git Integration', 'CI/CD Pipeline'],
  },
  rbac: {
    displayName: 'RBAC & Authentifizierung',
    description: 'Rollen- und Berechtigungsverwaltung',
    primaryServices: ['backend', 'consent-service'],
    databases: ['postgres', 'valkey'],
    tables: ['users', 'roles', 'permissions', 'role_permissions'],
    dataFlow: ['Browser', 'Next.js', 'FastAPI', 'JWT/Session', 'PostgreSQL'],
    dependencies: ['Valkey Session', 'bcrypt'],
  },
  communication: {
    displayName: 'Kommunikation',
    description: 'Matrix Messenger & Jitsi Video',
    primaryServices: ['matrix', 'jitsi'],
    databases: ['postgres'],
    tables: ['matrix_rooms', 'jitsi_meetings'],
    dataFlow: ['Browser', 'Matrix Synapse', 'Jitsi Meet', 'PostgreSQL'],
    dependencies: ['Matrix Federation', 'STUN/TURN'],
  },
  mail: {
    displayName: 'E-Mail Management',
    description: 'IMAP/SMTP mit KI-Analyse',
    primaryServices: ['backend', 'mailserver'],
    databases: ['postgres'],
    tables: ['mail_accounts', 'mail_messages', 'mail_analysis'],
    dataFlow: ['Mailserver (IMAP)', 'FastAPI', 'LLM Analysis', 'PostgreSQL'],
    dependencies: ['IMAP Auth', 'SMTP Auth', 'LLM Service'],
  },
  gpu: {
    displayName: 'GPU Infrastruktur',
    description: 'CUDA/ROCm GPU Management',
    primaryServices: ['backend'],
    databases: ['postgres'],
    tables: ['gpu_nodes', 'gpu_jobs', 'gpu_metrics'],
    dataFlow: ['Browser', 'FastAPI', 'NVIDIA/AMD Driver', 'GPU Hardware'],
    dependencies: ['CUDA', 'ROCm', 'nvidia-smi'],
  },
  llm: {
    displayName: 'LLM Vergleich',
    description: 'Vergleich verschiedener LLM Provider',
    primaryServices: ['backend'],
    databases: ['postgres'],
    tables: ['llm_providers', 'llm_benchmarks', 'llm_costs'],
    externalTools: ['OpenAI API', 'Anthropic API', 'Local LLMs'],
    dataFlow: ['Browser', 'FastAPI', 'LLM Provider API', 'PostgreSQL'],
    dependencies: ['API Keys', 'Token Counting'],
  },
  rag: {
    displayName: 'RAG & BYOEH',
    description: 'Retrieval Augmented Generation mit Client-Side Encryption',
    primaryServices: ['backend', 'klausur-frontend'],
    databases: ['postgres', 'qdrant'],
    tables: ['erwartungshorizonte', 'eh_chunks', 'documents', 'embeddings'],
    externalTools: ['Qdrant Vector DB', 'BGE-M3 Embeddings', 'BGE Reranker'],
    dataFlow: ['EH Upload', 'Client-Side Encryption', 'Chunking', 'Embedding', 'Vector Store', 'RAG Search', 'Client-Side Decryption'],
    dependencies: ['AES-256-GCM', 'PBKDF2', 'Web Crypto API', 'Qdrant'],
  },
  sbom: {
    displayName: 'SBOM Management',
    description: 'Software Bill of Materials',
    primaryServices: ['backend'],
    databases: ['postgres'],
    tables: ['sbom_components', 'vulnerabilities', 'licenses'],
    externalTools: ['syft', 'cyclonedx'],
    dataFlow: ['Container Image', 'SBOM Generator', 'FastAPI', 'PostgreSQL'],
    dependencies: ['Container Runtime', 'CVE Database'],
  },
  docs: {
    displayName: 'Entwickler-Dokumentation',
    description: 'Projektdokumentation & Guides',
    primaryServices: ['frontend'],
    databases: [],
    dataFlow: ['Markdown Files', 'Next.js', 'Browser'],
    dependencies: ['File System'],
  },
  'unity-bridge': {
    displayName: 'Unity AI Bridge',
    description: 'REST API zur externen Steuerung des Unity Editors',
    primaryServices: ['unity-bridge'],
    databases: [],
    dataFlow: ['Admin Panel', 'API Proxy', 'Unity Bridge', 'Unity Editor'],
    dependencies: ['Unity Editor muss laufen', 'Bridge Server gestartet'],
  },
}

// ==============================================
// Hilfsfunktionen
// ==============================================

export function getServiceInfo(serviceId: string): ServiceNode | undefined {
  return ARCHITECTURE_SERVICES[serviceId]
}

export function getModuleArchitecture(moduleId: string): ModuleArchitecture | undefined {
  return MODULE_ARCHITECTURE[moduleId]
}

export function getAllServices(moduleId: string): ServiceNode[] {
  const module = MODULE_ARCHITECTURE[moduleId]
  if (!module) return []

  const serviceIds = [...module.primaryServices, ...module.databases]
  return serviceIds
    .map((id) => ARCHITECTURE_SERVICES[id])
    .filter((s): s is ServiceNode => s !== undefined)
}

export function getDependencyChain(moduleId: string): string[] {
  const module = MODULE_ARCHITECTURE[moduleId]
  if (!module) return []

  return module.dataFlow
}

// ==============================================
// Wizard-spezifische Konfiguration
// ==============================================

export interface WizardModuleConfig {
  module: string
  available: boolean
  steps: number
  priority: 'high' | 'medium' | 'low'
}

export const AVAILABLE_WIZARDS: WizardModuleConfig[] = [
  { module: 'middleware', available: true, steps: 8, priority: 'high' },
  { module: 'consent', available: true, steps: 6, priority: 'high' },
  { module: 'dsr', available: true, steps: 7, priority: 'high' },
  { module: 'security', available: true, steps: 8, priority: 'high' },
  { module: 'rbac', available: true, steps: 6, priority: 'medium' },
  { module: 'communication', available: true, steps: 5, priority: 'medium' },
  { module: 'mail', available: true, steps: 6, priority: 'medium' },
  { module: 'unity-bridge', available: true, steps: 7, priority: 'medium' },
  { module: 'gpu', available: true, steps: 4, priority: 'low' },
  { module: 'llm', available: true, steps: 4, priority: 'low' },
  { module: 'rag', available: true, steps: 6, priority: 'high' },
]
