"use strict";
/**
 * 🧡 Trust Cache System - Real-time AI Verification Integration
 * Caches AI verification results for instant Trust Dashboard updates
 */
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.saveTrustCache = saveTrustCache;
exports.getTrustCache = getTrustCache;
exports.clearTrustCache = clearTrustCache;
const fs_1 = require("fs");
const path_1 = __importDefault(require("path"));
const os_1 = __importDefault(require("os"));
/**
 * Get trust cache file path
 */
function getTrustCachePath() {
    const cacheDir = path_1.default.join(os_1.default.homedir(), '.faf-cli-cache');
    return path_1.default.join(cacheDir, 'trust-cache.json');
}
/**
 * Ensure cache directory exists
 */
async function ensureCacheDir() {
    const cacheDir = path_1.default.dirname(getTrustCachePath());
    try {
        await fs_1.promises.mkdir(cacheDir, { recursive: true });
    }
    catch {
        // Directory already exists, ignore
    }
}
/**
 * Save verification results to cache
 */
async function saveTrustCache(fafPath, results) {
    try {
        await ensureCacheDir();
        // Calculate scores from verification results
        const scores = {
            claude: results.find(r => r.model === 'claude')?.confidence || 0,
            chatgpt: results.find(r => r.model === 'chatgpt')?.confidence || 0,
            gemini: results.find(r => r.model === 'gemini')?.confidence || 0
        };
        const average = Math.round((scores.claude + scores.chatgpt + scores.gemini) / 3);
        const allPassed = results.every(r => r.understood);
        const cacheData = {
            fafPath: path_1.default.resolve(fafPath),
            timestamp: Date.now(),
            aiCompatibilityScore: average,
            verificationResults: {
                ...scores,
                average
            },
            allPassed
        };
        // Read existing cache
        let cache = {};
        try {
            const existing = await fs_1.promises.readFile(getTrustCachePath(), 'utf-8');
            cache = JSON.parse(existing);
        }
        catch {
            // Cache doesn't exist yet, start fresh
        }
        // Update cache with new results
        cache[path_1.default.resolve(fafPath)] = cacheData;
        // Save updated cache
        await fs_1.promises.writeFile(getTrustCachePath(), JSON.stringify(cache, null, 2));
    }
    catch (error) {
        // Fail silently - don't break the verification if cache fails
        console.error('Failed to save trust cache:', error);
    }
}
/**
 * Get cached verification results
 */
async function getTrustCache(fafPath) {
    try {
        const cachePath = getTrustCachePath();
        const cacheData = await fs_1.promises.readFile(cachePath, 'utf-8');
        const cache = JSON.parse(cacheData);
        const resolvedPath = path_1.default.resolve(fafPath);
        const cached = cache[resolvedPath];
        if (!cached) {
            return null;
        }
        // Check if cache is still fresh (within 1 hour)
        const oneHour = 60 * 60 * 1000;
        if (Date.now() - cached.timestamp > oneHour) {
            return null; // Cache is stale
        }
        return cached;
    }
    catch {
        return null; // Cache doesn't exist or is corrupted
    }
}
/**
 * Clear trust cache for a specific file or all files
 */
async function clearTrustCache(fafPath) {
    try {
        if (!fafPath) {
            // Clear entire cache
            await fs_1.promises.unlink(getTrustCachePath());
            return;
        }
        // Remove specific file from cache
        const cacheData = await fs_1.promises.readFile(getTrustCachePath(), 'utf-8');
        const cache = JSON.parse(cacheData);
        const resolvedPath = path_1.default.resolve(fafPath);
        delete cache[resolvedPath];
        await fs_1.promises.writeFile(getTrustCachePath(), JSON.stringify(cache, null, 2));
    }
    catch {
        // Cache doesn't exist, nothing to clear
    }
}
//# sourceMappingURL=trust-cache.js.map