/**
 * Shared Types & Constants for Infrastructure/DevOps Modules
 *
 * Diese Datei enthaelt gemeinsame Typen und Konstanten fuer die DevOps-Pipeline:
 * - CI/CD: Woodpecker Pipelines & Deployments
 * - Tests: Test Dashboard & Backlog
 * - SBOM: Software Bill of Materials & Lizenz-Checks
 * - Security: DevSecOps Scans & Vulnerabilities
 *
 * Pipeline-Flow: CI/CD → Tests → SBOM → Security
 */

// =============================================================================
// DevOps Module IDs
// =============================================================================

/**
 * Eindeutige IDs fuer DevOps-Module in der Sidebar
 */
export type DevOpsToolId = 'ci-cd' | 'tests' | 'sbom' | 'security' | 'middleware' | 'gpu'

/**
 * Module die zur DevOps-Pipeline gehoeren
 */
export const DEVOPS_PIPELINE_IDS: DevOpsToolId[] = ['ci-cd', 'tests', 'sbom', 'security']

// =============================================================================
// Shared Status Types
// =============================================================================

/**
 * Pipeline-Status fuer CI/CD
 */
export type PipelineStatus = 'pending' | 'running' | 'success' | 'failure' | 'error' | 'blocked' | 'skipped'

/**
 * Quelle fuer Backlog-Eintraege
 */
export type BacklogSource = 'manual' | 'ci_cd' | 'security' | 'sbom'

/**
 * Kategorien fuer extrahierte Fehler
 */
export type ErrorCategory = 'test_failure' | 'build_error' | 'security_warning' | 'license_violation' | 'dependency_issue'

/**
 * LLM-Routing-Optionen
 */
export type LLMRoutingOption = 'local_only' | 'claude_preferred' | 'smart_routing'

/**
 * Sensitivity-Level fuer Privacy Classifier
 */
export type SensitivityLevel = 'sensitive' | 'non_sensitive' | 'unknown'

// =============================================================================
// Module Navigation
// =============================================================================

/**
 * Link-Definition fuer DevOps-Modul-Navigation
 */
export interface DevOpsModule {
  /** Eindeutige ID des Moduls */
  id: DevOpsToolId
  /** Anzeigename */
  name: string
  /** Navigation href */
  href: string
  /** Kurzbeschreibung */
  description: string
  /** Icon (Emoji) */
  icon: string
  /** Optional: Subgroup fuer Gruppierung */
  subgroup?: 'pipeline' | 'infrastructure'
}

/**
 * DevOps-Pipeline Module
 * Diese Module bilden eine zusammenhaengende Pipeline mit Datenfluss.
 * Datenfluss: CI/CD → Tests → SBOM → Security
 */
export const DEVOPS_PIPELINE_MODULES: DevOpsModule[] = [
  {
    id: 'ci-cd',
    name: 'CI/CD Pipelines',
    href: '/infrastructure/ci-cd',
    description: 'Gitea Actions & Deployments',
    icon: '🔄',
    subgroup: 'pipeline',
  },
  {
    id: 'tests',
    name: 'Test Dashboard',
    href: '/infrastructure/tests',
    description: '280+ Tests & Coverage',
    icon: '✅',
    subgroup: 'pipeline',
  },
  {
    id: 'sbom',
    name: 'SBOM',
    href: '/infrastructure/sbom',
    description: 'Dependencies & Licenses',
    icon: '📦',
    subgroup: 'pipeline',
  },
  {
    id: 'security',
    name: 'Security',
    href: '/infrastructure/security',
    description: 'DevSecOps & Scans',
    icon: '🛡️',
    subgroup: 'pipeline',
  },
]

/**
 * Andere Infrastructure-Module (nicht Teil der Pipeline)
 */
export const OTHER_INFRASTRUCTURE_MODULES: DevOpsModule[] = [
  {
    id: 'middleware',
    name: 'Middleware',
    href: '/infrastructure/middleware',
    description: 'API Gateway & Rate Limiting',
    icon: '🔗',
    subgroup: 'infrastructure',
  },
  {
    id: 'gpu',
    name: 'GPU',
    href: '/infrastructure/gpu',
    description: 'GPU Monitoring & Management',
    icon: '🖥️',
    subgroup: 'infrastructure',
  },
]

/**
 * Alle Infrastructure-Module
 */
export const ALL_INFRASTRUCTURE_MODULES: DevOpsModule[] = [
  ...DEVOPS_PIPELINE_MODULES,
  ...OTHER_INFRASTRUCTURE_MODULES,
]

// =============================================================================
// Data Flow Types
// =============================================================================

/**
 * Datenfluss-Verbindung zwischen Modulen
 */
export interface DataFlowConnection {
  from: DevOpsToolId
  to: DevOpsToolId
  label: string
  description: string
}

/**
 * Definiert den Datenfluss zwischen den DevOps-Modulen
 *
 * CI/CD → Tests: Pipeline-Ergebnisse fuellen Test Dashboard
 * Tests → Backlog: Fehlgeschlagene Tests → Backlog (automatisch bei CI/CD Failure)
 * CI/CD → SBOM: Nach erfolgreichem Build SBOM generieren
 * SBOM → Security: SBOM-Daten fuer Vulnerability-Checks
 */
export const DEVOPS_DATA_FLOW_CONNECTIONS: DataFlowConnection[] = [
  {
    from: 'ci-cd',
    to: 'tests',
    label: 'Test Results',
    description: 'Pipeline-Ergebnisse fuellen Test Dashboard',
  },
  {
    from: 'ci-cd',
    to: 'sbom',
    label: 'Build Artifacts',
    description: 'Nach erfolgreichem Build SBOM generieren (Syft)',
  },
  {
    from: 'sbom',
    to: 'security',
    label: 'Dependencies',
    description: 'SBOM-Daten fuer Vulnerability-Scanning (Grype)',
  },
]

// =============================================================================
// Log Extraction Types
// =============================================================================

/**
 * Extrahierter Fehler aus Pipeline-Logs
 */
export interface ExtractedError {
  /** Pipeline-Step in dem der Fehler auftrat */
  step: string
  /** Zeilen-Nummer im Log */
  line: number
  /** Fehlermeldung */
  message: string
  /** Kategorie des Fehlers */
  category: ErrorCategory
  /** Von LLM generierter Korrekturvorschlag */
  suggested_fix?: string
  /** Datei in der der Fehler auftrat (falls bekannt) */
  file_path?: string
  /** Service-Name (falls bekannt) */
  service?: string
}

/**
 * Response von Log-Extraktion Endpoint
 */
export interface LogExtractionResponse {
  /** Liste der extrahierten Fehler */
  errors: ExtractedError[]
  /** Pipeline-Nummer */
  pipeline_number: number
  /** Zeitpunkt der Extraktion */
  extracted_at: string
  /** Anzahl geparster Log-Zeilen */
  lines_parsed: number
}

// =============================================================================
// Webhook Types
// =============================================================================

/**
 * Woodpecker Webhook Event Types
 */
export type WoodpeckerEventType = 'pipeline_success' | 'pipeline_failure' | 'pipeline_started'

/**
 * Woodpecker Webhook Payload
 */
export interface WoodpeckerWebhookPayload {
  event: WoodpeckerEventType
  repo_id: number
  pipeline_number: number
  branch?: string
  commit?: string
  author?: string
  message?: string
}

// =============================================================================
// LLM Integration Types
// =============================================================================

/**
 * LLM-Analyse Request
 */
export interface LLMAnalysisRequest {
  /** Text/Code der analysiert werden soll */
  content: string
  /** Kontext (z.B. Fehlermeldung, Test-Name) */
  context?: string
  /** Routing-Praeferenz */
  routing: LLMRoutingOption
}

/**
 * LLM-Analyse Response
 */
export interface LLMAnalysisResponse {
  /** Generierter Fix-Vorschlag */
  suggested_fix: string
  /** Erklaerung des Problems */
  explanation: string
  /** Welches LLM wurde verwendet */
  model_used: 'claude-sonnet' | 'qwen2.5-32b' | 'other'
  /** Grund fuer Routing-Entscheidung */
  routing_reason?: string
  /** Confidence Score (0-1) */
  confidence?: number
}

/**
 * Privacy Classifier Result
 */
export interface PrivacyClassifierResult {
  /** Sensitivity-Level */
  level: SensitivityLevel
  /** Gefundene sensitive Patterns */
  patterns_found: string[]
  /** Empfohlenes Routing */
  recommended_routing: 'local' | 'external'
}

// =============================================================================
// Sidebar Props
// =============================================================================

/**
 * Props fuer DevOpsPipelineSidebar
 */
export interface DevOpsPipelineSidebarProps {
  /** Aktuell aktives Tool */
  currentTool: DevOpsToolId
  /** Kompakte Ansicht (collapsed by default) */
  compact?: boolean
  /** Zusaetzliche CSS-Klassen */
  className?: string
}

/**
 * Props fuer responsive Sidebar-Variante
 */
export interface DevOpsPipelineSidebarResponsiveProps extends DevOpsPipelineSidebarProps {
  /** FAB-Position auf Mobile */
  fabPosition?: 'bottom-right' | 'bottom-left'
}

// =============================================================================
// Live Status Types (fuer Badges)
// =============================================================================

/**
 * Live-Status fuer Sidebar-Badges
 */
export interface PipelineLiveStatus {
  /** Aktuelle Pipeline laeuft */
  isRunning: boolean
  /** Letzte Pipeline erfolgreich */
  lastSuccess: boolean
  /** Anzahl offener Backlog-Items */
  backlogCount: number
  /** Anzahl Security-Findings */
  securityFindingsCount: number
  /** Letzte Aktualisierung */
  lastUpdated: string
}

// =============================================================================
// API Configuration
// =============================================================================

/**
 * Backend-Endpunkte fuer die Infrastructure-Module
 */
export const INFRASTRUCTURE_API_ENDPOINTS = {
  /** CI/CD Endpoints */
  CI_CD: {
    PIPELINES: '/api/admin/infrastructure/woodpecker',
    TRIGGER: '/api/admin/infrastructure/woodpecker/trigger',
    LOGS: '/api/admin/infrastructure/woodpecker/logs',
  },
  /** Log Extraction Endpoints */
  LOG_EXTRACT: {
    EXTRACT: '/api/infrastructure/log-extract/extract',
  },
  /** Webhook Endpoints */
  WEBHOOKS: {
    WOODPECKER: '/api/webhooks/woodpecker',
  },
  /** LLM Endpoints */
  LLM: {
    ANALYZE: '/api/ai/analyze',
    LOCAL: '/api/ai/local-llm',
    CLASSIFY: '/api/ai/privacy-classify',
  },
} as const

// =============================================================================
// Architecture Documentation
// =============================================================================

/**
 * Architektur-Information fuer Dokumentationszwecke
 */
export const DEVOPS_ARCHITECTURE = {
  services: [
    { name: 'Woodpecker CI', port: 8000, description: 'CI/CD Pipeline Server' },
    { name: 'Gitea', port: 3003, description: 'Git Repository Server' },
    { name: 'Syft', type: 'CLI', description: 'SBOM Generator' },
    { name: 'Grype', type: 'CLI', description: 'Vulnerability Scanner' },
    { name: 'Semgrep', type: 'CLI', description: 'Static Analysis' },
    { name: 'Gitleaks', type: 'CLI', description: 'Secret Detection' },
  ],
  workflow: {
    description: 'CI/CD Pipeline Flow',
    steps: [
      { name: 'Code Push', icon: '📝' },
      { name: 'Build', icon: '🏗️' },
      { name: 'Test', icon: '✅' },
      { name: 'SBOM', icon: '📦' },
      { name: 'Security', icon: '🛡️' },
      { name: 'Deploy', icon: '🚀' },
    ],
  },
}
