#!/usr/bin/env node
"use strict";
/**
 * Agent Validation Script
 * Validates all agent files have required frontmatter and structure
 */
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.validateAgents = validateAgents;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
function validateFrontmatter(content) {
    // Check for frontmatter
    const frontmatterMatch = content.match(/^---\n([\s\S]*?)\n---/);
    if (!frontmatterMatch) {
        return { valid: false, error: 'Missing frontmatter (---)' };
    }
    const lines = frontmatterMatch[1].split('\n');
    const frontmatter = {};
    for (const line of lines) {
        const match = line.match(/^(\w+):\s*(.*)$/);
        if (match) {
            frontmatter[match[1]] = match[2].trim();
        }
    }
    // Validate required fields
    const required = ['name', 'description'];
    for (const field of required) {
        if (!frontmatter[field]) {
            return { valid: false, error: `Missing required field: ${field}` };
        }
    }
    // Validate optional fields format
    if (frontmatter.color && !frontmatter.color.match(/^#[0-9A-Fa-f]{6}$/)) {
        return { valid: false, error: 'Invalid color format (should be #RRGGBB)' };
    }
    return { valid: true, data: frontmatter };
}
function validateContent(content, _frontmatter) {
    // Check if has main content after frontmatter
    const mainContent = content.replace(/^---\n[\s\S]*?\n---/, '').trim();
    if (mainContent.length < 100) {
        return { valid: false, error: 'Content too short (min 100 chars)' };
    }
    // Check for common sections
    const hasIdentity = /##.*Identity|Your Identity/i.test(content);
    const hasMission = /##.*Mission|Core Mission/i.test(content);
    const hasRules = /##.*Rules|Critical Rules/i.test(content);
    if (!hasIdentity) {
        return { valid: false, error: 'Missing Identity section' };
    }
    if (!hasMission) {
        return { valid: false, error: 'Missing Mission section' };
    }
    if (!hasRules) {
        return { valid: false, error: 'Missing Rules section' };
    }
    return { valid: true };
}
async function validateAgents(_fix = false) {
    const agentsDir = path.join(process.cwd(), 'agents');
    const results = [];
    function validateFile(filePath) {
        const relativePath = path.relative(process.cwd(), filePath);
        const content = fs.readFileSync(filePath, 'utf-8');
        // Validate frontmatter
        const frontmatterResult = validateFrontmatter(content);
        if (!frontmatterResult.valid) {
            return { file: relativePath, valid: false, error: frontmatterResult.error };
        }
        // Validate content structure
        const contentResult = validateContent(content, frontmatterResult.data);
        if (!contentResult.valid) {
            return { file: relativePath, valid: false, error: contentResult.error };
        }
        return { file: relativePath, valid: true };
    }
    function scanDir(dir) {
        const entries = fs.readdirSync(dir, { withFileTypes: true });
        for (const entry of entries) {
            const fullPath = path.join(dir, entry.name);
            if (entry.isDirectory()) {
                scanDir(fullPath);
            }
            else if (entry.name.endsWith('.md')) {
                results.push(validateFile(fullPath));
            }
        }
    }
    scanDir(agentsDir);
    return {
        total: results.length,
        valid: results.filter(r => r.valid),
        invalid: results.filter(r => !r.valid)
    };
}
// CLI execution
if (require.main === module) {
    const fix = process.argv.includes('--fix') || process.argv.includes('-f');
    validateAgents(fix)
        .then(report => {
        console.log('\n📋 Validation Results\n');
        console.log(`   Total agents: ${report.total}`);
        console.log(`   ✅ Valid: ${report.valid.length}`);
        console.log(`   ❌ Invalid: ${report.invalid.length}`);
        if (report.invalid.length > 0) {
            console.log('\n   Issues found:');
            report.invalid.forEach(issue => {
                console.log(`   ❌ ${issue.file}`);
                console.log(`      ${issue.error}`);
            });
            process.exit(1);
        }
        else {
            console.log('\n✅ All agents are valid!');
            process.exit(0);
        }
    })
        .catch(error => {
        console.error('❌ Validation failed:', error);
        process.exit(1);
    });
}
//# sourceMappingURL=validate-agents.js.map