// ═══════════════════════════════════════════════════════════
// Pipeline Stage Constants — from CONTRACTS.md §4.3, Appendix A
// ═══════════════════════════════════════════════════════════

import type { PipelineStage } from "../types/pipeline.js";

export interface KanbanStageConfig {
  stage: PipelineStage;
  label: string;
  color: string;
  isGate?: boolean;
}

export const KANBAN_STAGES: KanbanStageConfig[] = [
  { stage: "intake",       label: "📥 Intake",       color: "#666" },
  { stage: "scaffolding",  label: "🏗️ Scaffolding",  color: "#888" },
  { stage: "building",     label: "🔨 Building",     color: "#4488FF" },
  { stage: "testing",      label: "🧪 Testing",      color: "#FF8844" },
  { stage: "review",       label: "👁️ Review ★",     color: "#FFD700", isGate: true },
  { stage: "staging",      label: "🚀 Staging ★",    color: "#00AAFF", isGate: true },
  { stage: "production",   label: "🌍 Production ★", color: "#FF4444", isGate: true },
  { stage: "published",    label: "✅ Published",     color: "#00CC55" },
];

/** Ordered list of all pipeline stages */
export const PIPELINE_STAGES: PipelineStage[] = [
  "intake",
  "scaffolding",
  "building",
  "testing",
  "review",
  "staging",
  "production",
  "published",
];

/** Stages that require human approval to pass through */
export const HUMAN_GATE_STAGES: PipelineStage[] = [
  "review",
  "staging",
  "production",
];

/** Stage transition matrix: from → allowed targets */
export const STAGE_TRANSITIONS: Record<PipelineStage, PipelineStage[]> = {
  intake:      ["scaffolding"],
  scaffolding: ["building"],
  building:    ["testing"],
  testing:     ["review"],
  review:      ["staging"],
  staging:     ["production"],
  production:  ["published"],
  published:   [],
};

/** Get the next stage in sequence */
export function getNextStage(current: PipelineStage): PipelineStage | null {
  const idx = PIPELINE_STAGES.indexOf(current);
  return idx >= 0 && idx < PIPELINE_STAGES.length - 1
    ? PIPELINE_STAGES[idx + 1]
    : null;
}

/** Get stage index (0-based) */
export function getStageIndex(stage: PipelineStage): number {
  return PIPELINE_STAGES.indexOf(stage);
}

/** Check if a stage requires human approval */
export function isHumanGate(stage: PipelineStage): boolean {
  return HUMAN_GATE_STAGES.includes(stage);
}
