/**
 * Skill Activation Debug Logger
 *
 * Logs skill matching decisions when SPECWEAVE_DEBUG_SKILLS=1
 *
 * @module core/plugins/skill-activation-logger
 */
import * as fs from 'fs';
import * as path from 'path';
import { consoleLogger as logger } from '../../utils/logger.js';
/**
 * Skill Activation Logger
 */
export class SkillActivationLogger {
    constructor(projectRoot) {
        this.enabled = process.env.SPECWEAVE_DEBUG_SKILLS === '1';
        this.logFilePath = path.join(projectRoot, '.specweave/logs/skill-activation.log');
    }
    /**
     * Log a skill activation event
     */
    logActivation(event) {
        if (!this.enabled)
            return;
        const logEntry = this.formatLogEntry(event);
        // Log to console
        logger.info('Skill Activation Event:');
        logger.info(`  Prompt: "${event.prompt}"`);
        logger.info(`  Matched: ${event.matchedSkills.length} skills`);
        logger.info(`  Rejected: ${event.rejectedSkills.length} skills`);
        // Write to log file
        try {
            const logDir = path.dirname(this.logFilePath);
            if (!fs.existsSync(logDir)) {
                fs.mkdirSync(logDir, { recursive: true });
            }
            fs.appendFileSync(this.logFilePath, logEntry + '\n\n');
        }
        catch (error) {
            logger.error(`Failed to write skill activation log: ${error}`);
        }
    }
    /**
     * Format log entry
     */
    formatLogEntry(event) {
        const sections = [
            `[${event.timestamp}] Skill Activation`,
            `Prompt: "${event.prompt}"`
        ];
        if (event.matchedSkills.length > 0) {
            const matchedLines = event.matchedSkills.map(skill => `  - ${skill.fqn} (score: ${Math.round(skill.score * 100)}%)\n    Keywords: ${skill.matchedKeywords.join(', ')}`);
            sections.push('', '✅ Matched Skills:', ...matchedLines);
        }
        if (event.rejectedSkills.length > 0) {
            const rejectedLines = event.rejectedSkills.map(skill => `  - ${skill.fqn}: ${skill.reason}`);
            sections.push('', '❌ Rejected Skills:', ...rejectedLines);
        }
        return sections.join('\n');
    }
    /**
     * Clear log file
     */
    clearLogs() {
        if (fs.existsSync(this.logFilePath)) {
            fs.unlinkSync(this.logFilePath);
            logger.info('Skill activation logs cleared');
        }
    }
    /**
     * Get log file path
     */
    getLogPath() {
        return this.logFilePath;
    }
    /**
     * Check if logging is enabled
     */
    isEnabled() {
        return this.enabled;
    }
}
/**
 * Global logger instance
 */
let globalLogger = null;
/**
 * Get or create global logger
 */
export function getSkillActivationLogger(projectRoot) {
    if (!globalLogger) {
        if (!projectRoot) {
            throw new Error('Skill activation logger not initialized');
        }
        globalLogger = new SkillActivationLogger(projectRoot);
    }
    return globalLogger;
}
/**
 * Log skill activation (convenience function)
 */
export function logSkillActivation(prompt, matchedSkills, rejectedSkills = []) {
    try {
        getSkillActivationLogger(process.cwd()).logActivation({
            timestamp: new Date().toISOString(),
            prompt,
            matchedSkills,
            rejectedSkills
        });
    }
    catch {
        // Silently fail if logger not initialized
    }
}
//# sourceMappingURL=skill-activation-logger.js.map