import type { EvidenceType } from '@/lib/sdk'

export type DisplayEvidenceType = 'document' | 'screenshot' | 'log' | 'audit-report' | 'certificate'
export type DisplayFormat = 'pdf' | 'image' | 'text' | 'json'
export type DisplayStatus = 'valid' | 'expired' | 'pending-review'

export interface DisplayEvidence {
  id: string
  name: string
  description: string
  displayType: DisplayEvidenceType
  format: DisplayFormat
  controlId: string
  linkedRequirements: string[]
  linkedControls: string[]
  uploadedBy: string
  uploadedAt: Date
  validFrom: Date
  validUntil: Date | null
  status: DisplayStatus
  fileSize: string
  fileUrl: string | null
  // Anti-Fake-Evidence Phase 2
  confidenceLevel: string | null
  truthStatus: string | null
  generationMode: string | null
  approvalStatus: string | null
  requiresFourEyes: boolean
}

export interface EvidenceTemplate {
  id: string
  name: string
  description: string
  type: EvidenceType
  displayType: DisplayEvidenceType
  format: DisplayFormat
  controlId: string
  linkedRequirements: string[]
  linkedControls: string[]
  uploadedBy: string
  validityDays: number
  fileSize: string
}

export interface EvidenceCheck {
  id: string
  check_code: string
  title: string
  description: string | null
  check_type: string
  target_url: string | null
  frequency: string
  is_active: boolean
  last_run_at: string | null
  next_run_at: string | null
}

export interface CheckResult {
  id: string
  check_id: string
  run_status: string
  summary: string | null
  findings_count: number
  critical_findings: number
  duration_ms: number
  run_at: string
}

export interface EvidenceMapping {
  id: string
  evidence_id: string
  control_code: string
  mapping_type: string
  verified_at: string | null
  verified_by: string | null
  notes: string | null
}

export interface CoverageReport {
  total_controls: number
  controls_with_evidence: number
  controls_without_evidence: number
  coverage_percent: number
}

export type EvidenceTabKey = 'evidence' | 'checks' | 'mapping' | 'report'

export const CHECK_TYPE_LABELS: Record<string, { label: string; color: string }> = {
  tls_scan: { label: 'TLS-Scan', color: 'bg-blue-100 text-blue-700' },
  header_check: { label: 'Header-Check', color: 'bg-green-100 text-green-700' },
  certificate_check: { label: 'Zertifikat', color: 'bg-yellow-100 text-yellow-700' },
  dns_check: { label: 'DNS-Check', color: 'bg-purple-100 text-purple-700' },
  api_scan: { label: 'API-Scan', color: 'bg-indigo-100 text-indigo-700' },
  config_scan: { label: 'Config-Scan', color: 'bg-orange-100 text-orange-700' },
  port_scan: { label: 'Port-Scan', color: 'bg-red-100 text-red-700' },
}

export const RUN_STATUS_LABELS: Record<string, { label: string; color: string }> = {
  running: { label: 'Laeuft...', color: 'bg-blue-100 text-blue-700' },
  passed: { label: 'Bestanden', color: 'bg-green-100 text-green-700' },
  failed: { label: 'Fehlgeschlagen', color: 'bg-red-100 text-red-700' },
  warning: { label: 'Warnung', color: 'bg-yellow-100 text-yellow-700' },
  error: { label: 'Fehler', color: 'bg-red-100 text-red-700' },
}

export const confidenceFilterColors: Record<string, string> = {
  E0: 'bg-red-200 text-red-800',
  E1: 'bg-yellow-200 text-yellow-800',
  E2: 'bg-blue-200 text-blue-800',
  E3: 'bg-green-200 text-green-800',
  E4: 'bg-emerald-200 text-emerald-800',
}

export const CHECK_API = '/api/sdk/v1/compliance/evidence-checks'

export function mapEvidenceTypeToDisplay(type: EvidenceType): DisplayEvidenceType {
  switch (type) {
    case 'DOCUMENT': return 'document'
    case 'SCREENSHOT': return 'screenshot'
    case 'LOG': return 'log'
    case 'CERTIFICATE': return 'certificate'
    case 'AUDIT_REPORT': return 'audit-report'
    default: return 'document'
  }
}

export function getEvidenceStatus(validUntil: Date | null): DisplayStatus {
  if (!validUntil) return 'pending-review'
  const now = new Date()
  if (validUntil < now) return 'expired'
  return 'valid'
}

export const evidenceTemplates: EvidenceTemplate[] = [
  {
    id: 'ev-dse-001',
    name: 'Datenschutzerklaerung v2.3',
    description: 'Aktuelle Datenschutzerklaerung fuer Website und App',
    type: 'DOCUMENT',
    displayType: 'document',
    format: 'pdf',
    controlId: 'ctrl-org-001',
    linkedRequirements: ['req-gdpr-13', 'req-gdpr-14'],
    linkedControls: ['ctrl-org-001'],
    uploadedBy: 'DSB',
    validityDays: 365,
    fileSize: '245 KB',
  },
  {
    id: 'ev-pentest-001',
    name: 'Penetrationstest Report Q4/2024',
    description: 'Externer Penetrationstest durch Security-Partner',
    type: 'AUDIT_REPORT',
    displayType: 'audit-report',
    format: 'pdf',
    controlId: 'ctrl-tom-001',
    linkedRequirements: ['req-gdpr-32', 'req-iso-a12'],
    linkedControls: ['ctrl-tom-001', 'ctrl-tom-002', 'ctrl-det-001'],
    uploadedBy: 'IT Security Team',
    validityDays: 365,
    fileSize: '2.1 MB',
  },
  {
    id: 'ev-iso-cert',
    name: 'ISO 27001 Zertifikat',
    description: 'Zertifizierung des ISMS',
    type: 'CERTIFICATE',
    displayType: 'certificate',
    format: 'pdf',
    controlId: 'ctrl-tom-001',
    linkedRequirements: ['req-iso-4.1', 'req-iso-5.1'],
    linkedControls: [],
    uploadedBy: 'QM Abteilung',
    validityDays: 365,
    fileSize: '156 KB',
  },
  {
    id: 'ev-schulung-001',
    name: 'Schulungsnachweis Datenschutz 2024',
    description: 'Teilnehmerliste und Schulungsinhalt',
    type: 'DOCUMENT',
    displayType: 'document',
    format: 'pdf',
    controlId: 'ctrl-org-001',
    linkedRequirements: ['req-gdpr-39'],
    linkedControls: ['ctrl-org-001'],
    uploadedBy: 'HR Team',
    validityDays: 365,
    fileSize: '890 KB',
  },
  {
    id: 'ev-rbac-001',
    name: 'Access Control Screenshot',
    description: 'Nachweis der RBAC-Konfiguration',
    type: 'SCREENSHOT',
    displayType: 'screenshot',
    format: 'image',
    controlId: 'ctrl-tom-001',
    linkedRequirements: ['req-gdpr-32'],
    linkedControls: ['ctrl-tom-001'],
    uploadedBy: 'Admin',
    validityDays: 0,
    fileSize: '1.2 MB',
  },
  {
    id: 'ev-log-001',
    name: 'Audit Log Export',
    description: 'Monatlicher Audit-Log Export',
    type: 'LOG',
    displayType: 'log',
    format: 'json',
    controlId: 'ctrl-det-001',
    linkedRequirements: ['req-gdpr-32'],
    linkedControls: ['ctrl-det-001'],
    uploadedBy: 'System',
    validityDays: 90,
    fileSize: '4.5 MB',
  },
]
