import {
  resolveSkillsDir
} from "./chunk-6B6UN6SG.js";
import {
  SkillMetadataSchema
} from "./chunk-HIK6OEUF.js";

// src/persona/skill-executor.ts
import * as fs from "fs";
import * as path from "path";
import { parse } from "yaml";
function resolveOutputMode(mode, trigger) {
  if (mode !== "auto") return mode;
  return trigger === "manual" ? "inline" : "artifact";
}
function buildArtifactPath(projectPath, headSha) {
  const date = (/* @__PURE__ */ new Date()).toISOString().slice(0, 10);
  const sha = headSha?.slice(0, 7) ?? "unknown";
  return path.join(projectPath, ".harness", "reviews", `${date}-${sha}.md`);
}
function buildArtifactContent(skillName, trigger, headSha) {
  const date = (/* @__PURE__ */ new Date()).toISOString().slice(0, 10);
  return [
    "---",
    `skill: ${skillName}`,
    `trigger: ${trigger}`,
    `sha: ${headSha?.slice(0, 7) ?? "unknown"}`,
    `date: ${date}`,
    `assessment: pending`,
    "---",
    "",
    `# Review by ${skillName}`,
    "",
    "## Strengths",
    "",
    "- (review pending)",
    "",
    "## Issues",
    "",
    "### Critical",
    "",
    "- None identified",
    "",
    "### Important",
    "",
    "- None identified",
    "",
    "### Suggestions",
    "",
    "- None identified",
    "",
    "## Assessment",
    "",
    "Pending \u2014 skill execution scaffolded.",
    "",
    "## Harness Checks",
    "",
    "- (run harness validate, check-deps, check-docs to populate)",
    ""
  ].join("\n");
}
async function executeSkill(skillName, context) {
  const startTime = Date.now();
  const skillsDir = resolveSkillsDir();
  const skillDir = path.join(skillsDir, skillName);
  if (!fs.existsSync(skillDir)) {
    return {
      status: "fail",
      output: `Skill not found: ${skillName}`,
      durationMs: Date.now() - startTime
    };
  }
  const yamlPath = path.join(skillDir, "skill.yaml");
  if (!fs.existsSync(yamlPath)) {
    return {
      status: "fail",
      output: `skill.yaml not found for ${skillName}`,
      durationMs: Date.now() - startTime
    };
  }
  const raw = fs.readFileSync(yamlPath, "utf-8");
  const parsed = parse(raw);
  const metadataResult = SkillMetadataSchema.safeParse(parsed);
  if (!metadataResult.success) {
    return {
      status: "fail",
      output: `Invalid skill metadata: ${metadataResult.error.message}`,
      durationMs: Date.now() - startTime
    };
  }
  const skillMdPath = path.join(skillDir, "SKILL.md");
  if (!fs.existsSync(skillMdPath)) {
    return {
      status: "fail",
      output: `SKILL.md not found for ${skillName}`,
      durationMs: Date.now() - startTime
    };
  }
  const skillContent = fs.readFileSync(skillMdPath, "utf-8");
  const metadata = metadataResult.data;
  const resolvedMode = resolveOutputMode(context.outputMode, context.trigger);
  const output = `Skill ${metadata.name} (${metadata.type}) loaded.
Cognitive mode: ${metadata.cognitive_mode ?? "default"}
Content length: ${skillContent.length} chars
Trigger: ${context.trigger}
`;
  let artifactPath;
  if (resolvedMode === "artifact") {
    artifactPath = buildArtifactPath(context.projectPath, context.headSha);
    const artifactContent = buildArtifactContent(skillName, context.trigger, context.headSha);
    const dir = path.dirname(artifactPath);
    fs.mkdirSync(dir, { recursive: true });
    fs.writeFileSync(artifactPath, artifactContent, "utf-8");
  }
  return {
    status: "pass",
    output,
    ...artifactPath ? { artifactPath } : {},
    durationMs: Date.now() - startTime
  };
}

export {
  executeSkill
};
