import {
  resultToMcpResponse
} from "./chunk-EPUKTTJZ.js";
import {
  sanitizePath
} from "./chunk-W6Y7ZW3Y.js";

// src/mcp/tools/feedback.ts
var createSelfReviewDefinition = {
  name: "create_self_review",
  description: "Generate a checklist-based code review from a git diff, checking harness constraints, custom rules, and diff patterns",
  inputSchema: {
    type: "object",
    properties: {
      path: { type: "string", description: "Path to project root" },
      diff: { type: "string", description: "Git diff string to review" },
      customRules: {
        type: "array",
        items: { type: "object" },
        description: "Optional custom rules to apply during review"
      },
      maxFileSize: {
        type: "number",
        description: "Maximum number of lines changed per file before flagging"
      },
      maxFileCount: {
        type: "number",
        description: "Maximum number of changed files before flagging"
      }
    },
    required: ["path", "diff"]
  }
};
async function handleCreateSelfReview(input) {
  try {
    const { parseDiff, createSelfReview } = await import("./dist-TG63OKA5.js");
    const parseResult = parseDiff(input.diff);
    if (!parseResult.ok) {
      return resultToMcpResponse(parseResult);
    }
    const projectPath = sanitizePath(input.path);
    const config = {
      rootDir: projectPath,
      harness: {
        context: true,
        constraints: true,
        entropy: true
      },
      ...input.customRules ? { customRules: input.customRules } : {},
      diffAnalysis: {
        enabled: true,
        ...input.maxFileSize !== void 0 ? { maxFileSize: input.maxFileSize } : {},
        ...input.maxFileCount !== void 0 ? { maxChangedFiles: input.maxFileCount } : {}
      }
    };
    const { loadGraphStore } = await import("./graph-loader-TIVI6NQ7.js");
    const store = await loadGraphStore(projectPath);
    let graphData;
    if (store) {
      const { GraphFeedbackAdapter } = await import("./dist-PGQ32ZI7.js");
      const adapter = new GraphFeedbackAdapter(store);
      const changedFiles = parseResult.value.files.map((f) => f.path);
      const impact = adapter.computeImpactData(changedFiles);
      const harness = adapter.computeHarnessCheckData();
      graphData = {
        impact: {
          affectedTests: [...impact.affectedTests],
          affectedDocs: [...impact.affectedDocs],
          impactScope: impact.impactScope
        },
        harness: { ...harness }
      };
    }
    const result = await createSelfReview(
      parseResult.value,
      config,
      graphData
    );
    return resultToMcpResponse(result);
  } catch (error) {
    return {
      content: [
        {
          type: "text",
          text: `Error: ${error instanceof Error ? error.message : String(error)}`
        }
      ],
      isError: true
    };
  }
}
var analyzeDiffDefinition = {
  name: "analyze_diff",
  description: "Parse a git diff and check for forbidden patterns, oversized files, and missing test coverage",
  inputSchema: {
    type: "object",
    properties: {
      diff: { type: "string", description: "Git diff string to analyze" },
      path: {
        type: "string",
        description: "Path to project root (enables graph-enhanced analysis)"
      },
      forbiddenPatterns: {
        type: "array",
        items: { type: "string" },
        description: "List of regex patterns that are forbidden in the diff"
      },
      maxFileSize: {
        type: "number",
        description: "Maximum number of lines changed per file before flagging"
      },
      maxFileCount: {
        type: "number",
        description: "Maximum number of changed files before flagging"
      }
    },
    required: ["diff"]
  }
};
async function handleAnalyzeDiff(input) {
  try {
    const { parseDiff, analyzeDiff } = await import("./dist-TG63OKA5.js");
    const parseResult = parseDiff(input.diff);
    if (!parseResult.ok) {
      return resultToMcpResponse(parseResult);
    }
    const options = {
      enabled: true,
      ...input.forbiddenPatterns ? {
        forbiddenPatterns: input.forbiddenPatterns.map((pattern) => ({
          pattern,
          message: `Forbidden pattern matched: ${pattern}`,
          severity: "warning"
        }))
      } : {},
      ...input.maxFileSize !== void 0 ? { maxFileSize: input.maxFileSize } : {},
      ...input.maxFileCount !== void 0 ? { maxChangedFiles: input.maxFileCount } : {}
    };
    let graphImpactData;
    if (input.path) {
      try {
        const { loadGraphStore } = await import("./graph-loader-TIVI6NQ7.js");
        const store = await loadGraphStore(sanitizePath(input.path));
        if (store) {
          const { GraphFeedbackAdapter } = await import("./dist-PGQ32ZI7.js");
          const adapter = new GraphFeedbackAdapter(store);
          const changedFiles = parseResult.value.files.map((f) => f.path);
          const impact = adapter.computeImpactData(changedFiles);
          graphImpactData = {
            affectedTests: [...impact.affectedTests],
            affectedDocs: [...impact.affectedDocs],
            impactScope: impact.impactScope
          };
        }
      } catch {
      }
    }
    const result = await analyzeDiff(parseResult.value, options, graphImpactData);
    return resultToMcpResponse(result);
  } catch (error) {
    return {
      content: [
        {
          type: "text",
          text: `Error: ${error instanceof Error ? error.message : String(error)}`
        }
      ],
      isError: true
    };
  }
}
var requestPeerReviewDefinition = {
  name: "request_peer_review",
  description: "Spawn an agent subprocess to perform code review. Returns structured feedback with approval status. Timeout: 120 seconds.",
  inputSchema: {
    type: "object",
    properties: {
      path: { type: "string", description: "Path to project root" },
      agentType: {
        type: "string",
        enum: [
          "architecture-enforcer",
          "documentation-maintainer",
          "test-reviewer",
          "entropy-cleaner",
          "custom"
        ],
        description: "Type of agent to use for the peer review"
      },
      diff: { type: "string", description: "Git diff string to review" },
      context: { type: "string", description: "Optional additional context for the reviewer" }
    },
    required: ["path", "agentType", "diff"]
  }
};
async function handleRequestPeerReview(input) {
  try {
    const { parseDiff, requestPeerReview } = await import("./dist-TG63OKA5.js");
    const parseResult = parseDiff(input.diff);
    if (!parseResult.ok) {
      return resultToMcpResponse(parseResult);
    }
    const reviewContext = {
      files: parseResult.value.files.map((f) => f.path),
      diff: input.diff,
      ...input.context ? { metadata: { context: input.context } } : {}
    };
    try {
      const { loadGraphStore } = await import("./graph-loader-TIVI6NQ7.js");
      const store = await loadGraphStore(sanitizePath(input.path));
      if (store) {
        const { GraphFeedbackAdapter } = await import("./dist-PGQ32ZI7.js");
        const adapter = new GraphFeedbackAdapter(store);
        const changedFiles = parseResult.value.files.map((f) => f.path);
        const impactData = adapter.computeImpactData(changedFiles);
        reviewContext.metadata = {
          ...reviewContext.metadata,
          graphContext: impactData
        };
      }
    } catch {
    }
    const result = await requestPeerReview(input.agentType, reviewContext, {
      timeout: 12e4,
      wait: true
    });
    return resultToMcpResponse(result);
  } catch (error) {
    return {
      content: [
        {
          type: "text",
          text: `Error: ${error instanceof Error ? error.message : String(error)}`
        }
      ],
      isError: true
    };
  }
}

export {
  createSelfReviewDefinition,
  handleCreateSelfReview,
  analyzeDiffDefinition,
  handleAnalyzeDiff,
  requestPeerReviewDefinition,
  handleRequestPeerReview
};
