// ═══════════════════════════════════════════════════════════
// Resource: factory://config/templates
// ═══════════════════════════════════════════════════════════

import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import type { PipelineTemplateConfig } from "../types.js";

// Hardcoded templates — these are the canonical pipeline templates
const TEMPLATES: PipelineTemplateConfig[] = [
  {
    id: "mcp-server-standard",
    name: "MCP Server — Standard",
    description:
      "Full-featured MCP server pipeline with all 8 stages, comprehensive testing, " +
      "code review gate, staging deploy, and production deploy.",
    stages: [
      "intake",
      "scaffolding",
      "building",
      "testing",
      "review",
      "staging",
      "production",
      "published",
    ],
    defaultConfig: {
      runtime: "node",
      language: "typescript",
      testFramework: "vitest",
      minCoverage: 80,
      requireCodeReview: true,
      requireStagingDeploy: true,
    },
    requiredAssets: ["code", "config", "docs", "test_report"],
    validationRules: [
      { type: "test_coverage", config: { minPercent: 80 } },
      { type: "tests_passing", config: { minPassRate: 1.0 } },
      { type: "required_asset", config: { assetType: "docs" } },
      { type: "approval_count", config: { minApprovals: 1 } },
    ],
  },
  {
    id: "mcp-server-minimal",
    name: "MCP Server — Minimal",
    description:
      "Lightweight pipeline for simple MCP servers. Fewer gates, " +
      "lower coverage requirements. Good for quick iterations.",
    stages: [
      "intake",
      "scaffolding",
      "building",
      "testing",
      "review",
      "production",
      "published",
    ],
    defaultConfig: {
      runtime: "node",
      language: "typescript",
      testFramework: "vitest",
      minCoverage: 60,
      requireCodeReview: true,
      requireStagingDeploy: false,
    },
    requiredAssets: ["code", "config"],
    validationRules: [
      { type: "test_coverage", config: { minPercent: 60 } },
      { type: "tests_passing", config: { minPassRate: 0.95 } },
    ],
  },
  {
    id: "mcp-server-enterprise",
    name: "MCP Server — Enterprise",
    description:
      "Maximum rigor pipeline for enterprise-grade MCP servers. " +
      "Multi-approver gates, high coverage, security review, performance benchmarks.",
    stages: [
      "intake",
      "scaffolding",
      "building",
      "testing",
      "review",
      "staging",
      "production",
      "published",
    ],
    defaultConfig: {
      runtime: "node",
      language: "typescript",
      testFramework: "vitest",
      minCoverage: 95,
      requireCodeReview: true,
      requireStagingDeploy: true,
      requireSecurityReview: true,
      requirePerformanceBenchmarks: true,
      minApprovers: 2,
    },
    requiredAssets: ["code", "config", "docs", "test_report", "build"],
    validationRules: [
      { type: "test_coverage", config: { minPercent: 95 } },
      { type: "tests_passing", config: { minPassRate: 1.0 } },
      { type: "required_asset", config: { assetType: "docs" } },
      { type: "required_asset", config: { assetType: "test_report" } },
      { type: "approval_count", config: { minApprovals: 2 } },
      { type: "custom", config: { name: "security_review", required: true } },
      { type: "custom", config: { name: "perf_benchmarks", threshold: "p99 < 200ms" } },
    ],
  },
];

export function registerConfigResources(server: McpServer): void {
  server.resource(
    "config-templates",
    "factory://config/templates",
    {
      description: "Available pipeline templates and their configurations",
      mimeType: "application/json",
    },
    async () => {
      return {
        contents: [
          {
            uri: "factory://config/templates",
            mimeType: "application/json" as const,
            text: JSON.stringify(TEMPLATES, null, 2),
          },
        ],
      };
    },
  );
}
