"use strict";
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.OptimizationLoop = void 0;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const PerformanceScorecard_1 = require("./PerformanceScorecard");
class OptimizationLoop {
    filePath;
    scorecard;
    constructor() {
        this.filePath = path.join(process.cwd(), '.artifacts', 'optimization-suggestions.json');
        fs.mkdirSync(path.dirname(this.filePath), { recursive: true });
        this.scorecard = new PerformanceScorecard_1.PerformanceScorecard();
    }
    generateSuggestions() {
        const all = Object.values(this.scorecard.getAll());
        const suggestions = all
            .filter((item) => item.runs >= 2)
            .filter((item) => item.score < 1 || item.fallbackRate > 0.4 || item.avgReviewScore < 6)
            .map((item) => ({
            entityId: item.entityId,
            entityType: item.entityType,
            reason: `score=${item.score.toFixed(2)}, fallbackRate=${item.fallbackRate.toFixed(2)}, avgReviewScore=${item.avgReviewScore.toFixed(2)}`,
            suggestedAction: this.suggestAction(item),
            priority: item.score < 0 || item.avgReviewScore < 5 ? 'high' : 'medium',
            createdAt: new Date().toISOString()
        }));
        fs.writeFileSync(this.filePath, JSON.stringify(suggestions, null, 2), 'utf-8');
        return suggestions;
    }
    suggestAction(item) {
        if (item.fallbackRate > 0.4)
            return 'Reduzir dependência desta rota e testar fallback alternativo ou policy de seleção.';
        if (item.avgReviewScore < 6)
            return 'Revisar qualidade da saída e ajustar prompt/roteamento/skill associada.';
        return 'Reavaliar prioridade desta entidade no roteamento e comparar com alternativas melhores.';
    }
}
exports.OptimizationLoop = OptimizationLoop;
