import {
  resolveProjectConfig
} from "./chunk-6CHNY3NJ.js";
import {
  sanitizePath
} from "./chunk-W6Y7ZW3Y.js";

// src/mcp/tools/validate.ts
import * as path from "path";
var validateToolDefinition = {
  name: "validate_project",
  description: "Run all validation checks on a harness engineering project",
  inputSchema: {
    type: "object",
    properties: {
      path: { type: "string", description: "Path to project root directory" }
    },
    required: ["path"]
  }
};
async function handleValidateProject(input) {
  let projectPath;
  try {
    projectPath = sanitizePath(input.path);
  } catch (error) {
    return {
      content: [
        {
          type: "text",
          text: `Error: ${error instanceof Error ? error.message : String(error)}`
        }
      ],
      isError: true
    };
  }
  const errors = [];
  const checks = {
    config: "fail",
    structure: "skipped",
    agentsMap: "skipped"
  };
  const configResult = resolveProjectConfig(projectPath);
  if (!configResult.ok) {
    errors.push(`Config: ${configResult.error.message}`);
    return {
      content: [{ type: "text", text: JSON.stringify({ valid: false, checks, errors }) }]
    };
  }
  checks.config = "pass";
  const config = configResult.value;
  try {
    const core = await import("./dist-I4LWG3IN.js");
    if (typeof core.validateFileStructure === "function" && Array.isArray(config.conventions)) {
      const conventions = config.conventions;
      const structureResult = await core.validateFileStructure(projectPath, conventions);
      if (structureResult.ok) {
        checks.structure = structureResult.value.valid ? "pass" : "fail";
        if (!structureResult.value.valid) {
          for (const missing of structureResult.value.missing) {
            errors.push(`Missing required file: ${missing}`);
          }
        }
      } else {
        checks.structure = "fail";
        errors.push(`Structure validation error: ${structureResult.error.message}`);
      }
    }
  } catch {
  }
  try {
    const core = await import("./dist-I4LWG3IN.js");
    if (typeof core.validateAgentsMap === "function") {
      const agentsMapPath = path.join(projectPath, "AGENTS.md");
      const agentsResult = await core.validateAgentsMap(agentsMapPath);
      if (agentsResult.ok) {
        checks.agentsMap = agentsResult.value.valid ? "pass" : "fail";
        if (!agentsResult.value.valid) {
          if (agentsResult.value.missingSections.length > 0) {
            errors.push(
              `AGENTS.md missing sections: ${agentsResult.value.missingSections.join(", ")}`
            );
          }
          if (agentsResult.value.brokenLinks.length > 0) {
            errors.push(`AGENTS.md has ${agentsResult.value.brokenLinks.length} broken link(s)`);
          }
        }
      } else {
        checks.agentsMap = "fail";
        errors.push(`AGENTS.md validation error: ${agentsResult.error.message}`);
      }
    }
  } catch {
  }
  const valid = errors.length === 0;
  return {
    content: [{ type: "text", text: JSON.stringify({ valid, checks, errors }) }]
  };
}

export {
  validateToolDefinition,
  handleValidateProject
};
