// =============================================================================
// Pure skills API. Skills live in a Gitea repo at .skills/<name>/SKILL.md.
// Takes a GiteaReadConfig so it can read from any Gitea instance (in-process
// agent passes ctx.gitea, MCP server loads from env).
// =============================================================================

const SKILL_FILE = 'SKILL.md';
const SKILLS_DIR = '.skills';

export interface GiteaReadConfig {
    apiUrl: string;
    apiToken: string;
}

async function giteaGetContents(
    cfg: GiteaReadConfig,
    repo: string,
    filePath: string
): Promise<any> {
    const res = await fetch(`${cfg.apiUrl}/api/v1/repos/${repo}/contents/${filePath}`, {
        headers: { Authorization: `token ${cfg.apiToken}` },
    });
    if (!res.ok) return null;
    return res.json();
}

export async function listSkills(cfg: GiteaReadConfig, repo: string): Promise<unknown> {
    const contents = await giteaGetContents(cfg, repo, SKILLS_DIR);
    if (!contents || !Array.isArray(contents)) {
        return { skills: [], message: `No .skills/ directory found in ${repo}` };
    }
    const skills = contents
        .filter((entry: any) => entry.type === 'dir')
        .map((entry: any) => ({ name: entry.name, path: entry.path }));
    return { repo, skills };
}

export async function getSkill(cfg: GiteaReadConfig, repo: string, skillName: string): Promise<unknown> {
    const filePath = `${SKILLS_DIR}/${skillName}/${SKILL_FILE}`;
    const file = await giteaGetContents(cfg, repo, filePath);
    if (!file || !file.content) {
        return { error: `Skill "${skillName}" not found in ${repo}. Try list_skills to see available skills.` };
    }
    const content = Buffer.from(file.content, 'base64').toString('utf8');
    // Strip YAML frontmatter if present, return just the markdown body
    const body = content.replace(/^---[\s\S]*?---\s*/m, '').trim();
    return { repo, skill: skillName, content: body };
}
