// packages/plugin-awf/src/commands/list.ts
// AWF List Command - List available skills and workflows
import * as fs from "node:fs";
import * as path from "node:path";
import pc from "picocolors";
import { SKILLS_BY_CATEGORY } from "../constants/skills.js";
// Convert to format expected by display logic
const SKILLS_CATALOG = Object.entries(SKILLS_BY_CATEGORY).map(([category, skills]) => ({
    category,
    skills: [...skills],
}));
const WORKFLOWS_CATALOG = [
    { name: "init", description: "Initialize project" },
    { name: "code", description: "Write quality code" },
    { name: "debug", description: "Systematic debugging" },
    { name: "test", description: "Run and write tests" },
    { name: "deploy", description: "Deploy to production" },
    { name: "review", description: "Code review" },
    { name: "refactor", description: "Code refactoring" },
    { name: "plan", description: "Feature planning" },
    { name: "ap", description: "Full project audit" },
];
export const listFlags = {
    type: {
        type: "string",
        description: "Filter by type (skills/workflows/all)",
        alias: "t",
        choices: ["skills", "workflows", "all"],
        default: "all",
    },
    installed: {
        type: "boolean",
        description: "Show only installed skills",
        alias: "i",
        default: false,
    },
};
export async function listCommand(args, flags) {
    const cwd = process.cwd();
    const stateFile = path.join(cwd, ".agent/memory/state.json");
    const type = flags.type || "all";
    const showOnlyInstalled = flags.installed;
    // Read installed skills if state exists
    let installedSkills = [];
    if (fs.existsSync(stateFile)) {
        try {
            const state = JSON.parse(fs.readFileSync(stateFile, "utf-8"));
            installedSkills = Array.isArray(state.skills) ? state.skills : [];
        }
        catch {
            // Ignore parse errors
        }
    }
    console.log();
    // List skills
    if (type === "all" || type === "skills") {
        console.log(pc.bold(pc.cyan("📦 Skills")));
        console.log();
        for (const category of SKILLS_CATALOG) {
            const skills = showOnlyInstalled
                ? category.skills.filter((s) => installedSkills.includes(s))
                : category.skills;
            if (skills.length === 0)
                continue;
            console.log(pc.bold(`  ${category.category}:`));
            for (const skill of skills) {
                const installed = installedSkills.includes(skill);
                const marker = installed ? pc.green("✔") : pc.dim("○");
                console.log(`    ${marker} ${skill}`);
            }
            console.log();
        }
        const totalSkillCount = Object.values(SKILLS_BY_CATEGORY).reduce((sum, arr) => sum + arr.length, 0);
        if (installedSkills.length > 0) {
            console.log(pc.dim(`  Installed: ${installedSkills.length} | Total: ${totalSkillCount}`));
        }
        else {
            console.log(pc.dim("  No skills installed. Run: nock awf add"));
        }
        console.log();
    }
    // List workflows
    if (type === "all" || type === "workflows") {
        console.log(pc.bold(pc.cyan("🔄 Workflows")));
        console.log();
        for (const wf of WORKFLOWS_CATALOG) {
            console.log(`  ${pc.green(`/${wf.name}`)}${" ".repeat(12 - wf.name.length)} ${wf.description}`);
        }
        console.log();
    }
}
