// packages/plugin-awf/src/commands/add.ts
// AWF Add Command - Add skills to project
import * as fs from "node:fs";
import * as path from "node:path";
import pc from "picocolors";
import * as prompts from "@clack/prompts";
import { AVAILABLE_SKILLS } from "../constants/skills.js";
export async function addCommand(args, _flags) {
    const cwd = process.cwd();
    const stateFile = path.join(cwd, ".agent/memory/state.json");
    // Check if initialized
    if (!fs.existsSync(stateFile)) {
        console.log(pc.yellow("DOMYH not initialized. Run: nock awf init"));
        return;
    }
    // Read current state
    let state;
    try {
        state = JSON.parse(fs.readFileSync(stateFile, "utf-8"));
    }
    catch {
        console.log(pc.red("Failed to read state.json — file may be corrupted"));
        console.log(pc.dim("Re-initialize with: nock awf init"));
        return;
    }
    const currentSkills = state.skills || [];
    let skillsToAdd = [];
    // Skills from args
    if (args.length > 0) {
        skillsToAdd = args.filter((s) => AVAILABLE_SKILLS.some((as) => as.name === s));
        const invalid = args.filter((s) => !AVAILABLE_SKILLS.some((as) => as.name === s));
        if (invalid.length > 0) {
            console.log(pc.yellow(`Unknown skills: ${invalid.join(", ")}`));
            console.log(pc.dim(`Run: nock awf list to see available skills`));
        }
    }
    else {
        // Interactive selection
        const available = AVAILABLE_SKILLS.filter((s) => !currentSkills.includes(s.name));
        if (available.length === 0) {
            console.log(pc.green("All skills already installed!"));
            return;
        }
        const selected = await prompts.multiselect({
            message: "Select skills to add",
            options: available.map((s) => ({
                value: s.name,
                label: `${s.name} - ${s.description}`,
            })),
            required: true,
        });
        if (prompts.isCancel(selected)) {
            console.log(pc.yellow("Cancelled"));
            return;
        }
        skillsToAdd = selected;
    }
    if (skillsToAdd.length === 0) {
        console.log(pc.yellow("No skills to add"));
        return;
    }
    // Filter already installed
    const toInstall = skillsToAdd.filter((s) => !currentSkills.includes(s));
    const alreadyInstalled = skillsToAdd.filter((s) => currentSkills.includes(s));
    if (alreadyInstalled.length > 0) {
        console.log(pc.dim(`Already installed: ${alreadyInstalled.join(", ")}`));
    }
    if (toInstall.length === 0) {
        console.log(pc.green("All requested skills already installed!"));
        return;
    }
    // Update state
    state.skills = [...currentSkills, ...toInstall];
    fs.writeFileSync(stateFile, JSON.stringify(state, null, 2));
    console.log(pc.green(`✔ Added skills: ${toInstall.join(", ")}`));
    console.log(pc.dim(`Total skills: ${state.skills.length}`));
}
