// ── Pipeline Stages ───────────────────────────────────────────────────────────
export const PipelineStage = {
  STAGE_1_RAW_EVIDENCE: 'STAGE_1_RAW_EVIDENCE',
  STAGE_2_NORMALIZED: 'STAGE_2_NORMALIZED',
  STAGE_3_ENRICHED: 'STAGE_3_ENRICHED',
  STAGE_4_REVIEW_CANDIDATE: 'STAGE_4_REVIEW_CANDIDATE',
  STAGE_5_APPROVED_FUTURE: 'STAGE_5_APPROVED_FUTURE',
  STAGE_6_MATCHABLE_RESULT: 'STAGE_6_MATCHABLE_RESULT',
  STAGE_7_STRATEGIC_INPUT: 'STAGE_7_STRATEGIC_INPUT',
} as const
export type PipelineStage = typeof PipelineStage[keyof typeof PipelineStage]

export const PIPELINE_STAGE_LABELS: Record<PipelineStage, string> = {
  STAGE_1_RAW_EVIDENCE: 'Rohe Markt-Evidenz',
  STAGE_2_NORMALIZED: 'Normalisiertes Signal',
  STAGE_3_ENRICHED: 'Angereichertes Signal',
  STAGE_4_REVIEW_CANDIDATE: 'Review-Kandidat',
  STAGE_5_APPROVED_FUTURE: 'Genehmigtes Future Signal',
  STAGE_6_MATCHABLE_RESULT: 'Matchbares Ergebnis',
  STAGE_7_STRATEGIC_INPUT: 'Strategischer Entscheidungs-Input',
}

export const PIPELINE_STAGE_DESCRIPTIONS: Record<PipelineStage, string> = {
  STAGE_1_RAW_EVIDENCE: 'Rohe Evidenz aus Quellen gesammelt – noch unverarbeitet',
  STAGE_2_NORMALIZED: 'Daten normalisiert, Felder validiert und vereinheitlicht',
  STAGE_3_ENRICHED: 'Entitäten extrahiert, Kontext angereichert und bewertet',
  STAGE_4_REVIEW_CANDIDATE: 'Signal bereit für manuellen Analyst-Review',
  STAGE_5_APPROVED_FUTURE: 'Genehmigt als Future Availability Signal – intern sichtbar',
  STAGE_6_MATCHABLE_RESULT: 'Im Unified Result Feed – Match-Engine nutzbar',
  STAGE_7_STRATEGIC_INPUT: 'Eingang in Decision Briefs und strategische Analyse',
}

export const PIPELINE_STAGE_ORDER: PipelineStage[] = [
  'STAGE_1_RAW_EVIDENCE',
  'STAGE_2_NORMALIZED',
  'STAGE_3_ENRICHED',
  'STAGE_4_REVIEW_CANDIDATE',
  'STAGE_5_APPROVED_FUTURE',
  'STAGE_6_MATCHABLE_RESULT',
  'STAGE_7_STRATEGIC_INPUT',
]

// ── Gate Types ────────────────────────────────────────────────────────────────
export const GateType = {
  EVIDENCE_GATE: 'EVIDENCE_GATE',
  CONFIDENCE_GATE: 'CONFIDENCE_GATE',
  SENSITIVITY_GATE: 'SENSITIVITY_GATE',
  REVIEW_GATE: 'REVIEW_GATE',
  MATCHABILITY_GATE: 'MATCHABILITY_GATE',
  FEED_ELIGIBILITY_GATE: 'FEED_ELIGIBILITY_GATE',
} as const
export type GateType = typeof GateType[keyof typeof GateType]

export const GATE_LABELS: Record<GateType, string> = {
  EVIDENCE_GATE: 'Evidenz-Gate',
  CONFIDENCE_GATE: 'Konfidenz-Gate',
  SENSITIVITY_GATE: 'Sensitivitäts-Gate',
  REVIEW_GATE: 'Review-Gate',
  MATCHABILITY_GATE: 'Matchbarkeits-Gate',
  FEED_ELIGIBILITY_GATE: 'Feed-Eignung',
}

// ── Gate Status ───────────────────────────────────────────────────────────────
export const GateStatus = {
  PASSED: 'PASSED',
  FAILED: 'FAILED',
  PENDING: 'PENDING',
  BLOCKED: 'BLOCKED',
  SKIPPED: 'SKIPPED',
} as const
export type GateStatus = typeof GateStatus[keyof typeof GateStatus]

export const GATE_STATUS_LABELS: Record<GateStatus, string> = {
  PASSED: 'Bestanden',
  FAILED: 'Fehlgeschlagen',
  PENDING: 'Ausstehend',
  BLOCKED: 'Blockiert',
  SKIPPED: 'Übersprungen',
}

export const GATE_STATUS_COLORS: Record<GateStatus, { bg: string; fg: string }> = {
  PASSED:  { bg: 'rgba(22,163,74,0.1)',   fg: '#15803d' },
  FAILED:  { bg: 'rgba(239,68,68,0.1)',   fg: '#dc2626' },
  PENDING: { bg: 'rgba(234,179,8,0.1)',   fg: '#a16207' },
  BLOCKED: { bg: 'rgba(239,68,68,0.08)',  fg: '#dc2626' },
  SKIPPED: { bg: 'rgba(148,163,184,0.1)', fg: '#64748b' },
}

// ── Interfaces ────────────────────────────────────────────────────────────────
export interface GateCheck {
  label: string
  passed: boolean
  value?: string
  note?: string
}

export interface GateEvaluation {
  gateType: GateType
  status: GateStatus
  reason: string
  nextAction?: string
  evaluatedAt: string
  checks: GateCheck[]
}

export interface PipelineState {
  signalId: string
  currentStage: PipelineStage
  gates: Record<GateType, GateEvaluation>
  overallEligible: boolean
  publishedToFutureAvailability: boolean
  publishedAt?: string
  feedDisclaimer?: string
}

export interface AuditTrailEntry {
  id: string
  signalId: string
  timestamp: string
  stage: PipelineStage
  action: string
  performedBy: string
  details: string
  gateType?: GateType
}
