import fs from 'fs-extra';
import { readJsonFile } from '../lib/json.js';
import { getPackagePath, getStatePath } from '../lib/paths.js';
import { McpSchema, SkillSchema } from '../models/records.js';
export async function validateCuratedSkills() {
    try {
        const raw = await loadCuratedArray('data/curated/skills.json');
        const parsed = raw.map((entry, index) => {
            try {
                return SkillSchema.parse(entry);
            }
            catch (error) {
                throw new Error(`skills[${index}] ${error}`);
            }
        });
        return {
            dataset: 'skills',
            valid: true,
            count: parsed.length,
            errors: [],
            records: parsed
        };
    }
    catch (error) {
        return {
            dataset: 'skills',
            valid: false,
            count: 0,
            errors: [error instanceof Error ? error.message : String(error)]
        };
    }
}
export async function validateCuratedMcps(skillIds) {
    try {
        const raw = await loadCuratedArray('data/curated/mcps.json');
        const parsed = raw.map((entry, index) => {
            try {
                const record = McpSchema.parse(entry);
                const missingLinks = record.skillLinks.filter((link) => !skillIds.has(link));
                if (missingLinks.length) {
                    throw new Error(`references missing skills: ${missingLinks.join(', ')}`);
                }
                return record;
            }
            catch (error) {
                throw new Error(`mcps[${index}] ${error}`);
            }
        });
        return {
            dataset: 'mcps',
            valid: true,
            count: parsed.length,
            errors: [],
            records: parsed
        };
    }
    catch (error) {
        return {
            dataset: 'mcps',
            valid: false,
            count: 0,
            errors: [error instanceof Error ? error.message : String(error)]
        };
    }
}
async function loadCuratedArray(relativePath) {
    const statePath = getStatePath(relativePath);
    if (await fs.pathExists(statePath)) {
        return readJsonFile(statePath);
    }
    return readJsonFile(getPackagePath(relativePath));
}
