"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TaskAIContextEntity = exports.AIAgentType = exports.AIContextType = void 0;
const typeorm_1 = require("typeorm");
/**
 * Context types for AI interactions
 * Based on architecture.md#task_ai_contexts Collection
 */
var AIContextType;
(function (AIContextType) {
    AIContextType["PROMPT"] = "prompt";
    AIContextType["RESULT"] = "result";
    AIContextType["FEEDBACK"] = "feedback";
    AIContextType["ERROR"] = "error";
    AIContextType["UPDATE"] = "update";
})(AIContextType || (exports.AIContextType = AIContextType = {}));
/**
 * AI agent types supported by the system
 */
var AIAgentType;
(function (AIAgentType) {
    AIAgentType["OPENAI"] = "openai";
    AIAgentType["LANGCHAIN"] = "langchain";
    AIAgentType["NLP"] = "nlp";
    AIAgentType["CLAUDE_CODE"] = "claude_code";
    AIAgentType["BMAD_AGENT"] = "bmad_agent";
    AIAgentType["CUSTOM"] = "custom";
})(AIAgentType || (exports.AIAgentType = AIAgentType = {}));
/**
 * Entity for storing AI agent context and prompt data
 * Tracks all AI interactions for tasks including prompts, results, feedback, and errors
 *
 * Based on architecture.md#task_ai_contexts Collection (lines 213-224)
 */
let TaskAIContextEntity = class TaskAIContextEntity {
    // Helper methods
    /**
     * Calculate the cost based on token usage
     * @param pricePerToken Price per token in USD
     * @returns Estimated cost in USD
     */
    calculateCost(pricePerToken = 0.00002) {
        if (!this.tokens_used)
            return null;
        return this.tokens_used * pricePerToken;
    }
    /**
     * Check if this is an error context
     */
    isError() {
        return this.context_type === AIContextType.ERROR || !this.success;
    }
    /**
     * Get the total token count from context data
     */
    getTotalTokens() {
        if (this.tokens_used)
            return this.tokens_used;
        if (this.context_data?.totalTokens)
            return this.context_data.totalTokens;
        const prompt = this.context_data?.promptTokens || 0;
        const completion = this.context_data?.completionTokens || 0;
        return prompt + completion;
    }
    /**
     * Get a summary of the context for logging
     */
    getSummary() {
        const tokens = this.getTotalTokens();
        const time = this.execution_time_ms ? `${this.execution_time_ms}ms` : 'N/A';
        const status = this.success ? '✅' : '❌';
        return `${status} ${this.ai_agent_type} ${this.context_type} - Tokens: ${tokens}, Time: ${time}`;
    }
    /**
     * Validate the entity before saving
     */
    validate() {
        if (!this.task_id) {
            throw new Error('task_id is required');
        }
        if (!this.ai_agent_type) {
            throw new Error('ai_agent_type is required');
        }
        if (!this.context_type) {
            throw new Error('context_type is required');
        }
        if (!this.execution_timestamp) {
            this.execution_timestamp = new Date();
        }
        // Validate token usage is non-negative
        if (this.tokens_used !== null && this.tokens_used < 0) {
            throw new Error('tokens_used must be non-negative');
        }
        // Validate execution time is non-negative
        if (this.execution_time_ms !== null && this.execution_time_ms < 0) {
            throw new Error('execution_time_ms must be non-negative');
        }
    }
};
exports.TaskAIContextEntity = TaskAIContextEntity;
__decorate([
    (0, typeorm_1.PrimaryGeneratedColumn)('uuid'),
    __metadata("design:type", String)
], TaskAIContextEntity.prototype, "id", void 0);
__decorate([
    (0, typeorm_1.Column)({ type: 'uuid', name: 'task_id' }),
    (0, typeorm_1.Index)(),
    __metadata("design:type", String)
], TaskAIContextEntity.prototype, "task_id", void 0);
__decorate([
    (0, typeorm_1.Column)({
        type: 'varchar',
        length: 50,
        name: 'ai_agent_type',
    }),
    __metadata("design:type", String)
], TaskAIContextEntity.prototype, "ai_agent_type", void 0);
__decorate([
    (0, typeorm_1.Column)({
        type: 'varchar',
        length: 20,
        name: 'context_type',
    }),
    __metadata("design:type", String)
], TaskAIContextEntity.prototype, "context_type", void 0);
__decorate([
    (0, typeorm_1.Column)({
        type: 'simple-json',
        name: 'context_data',
        nullable: true,
    }),
    __metadata("design:type", Object)
], TaskAIContextEntity.prototype, "context_data", void 0);
__decorate([
    (0, typeorm_1.Column)({
        type: 'datetime',
        name: 'execution_timestamp',
    }),
    __metadata("design:type", Date)
], TaskAIContextEntity.prototype, "execution_timestamp", void 0);
__decorate([
    (0, typeorm_1.Column)({
        type: 'boolean',
        default: true,
    }),
    __metadata("design:type", Boolean)
], TaskAIContextEntity.prototype, "success", void 0);
__decorate([
    (0, typeorm_1.Column)({
        type: 'int',
        nullable: true,
        name: 'tokens_used',
    }),
    __metadata("design:type", Object)
], TaskAIContextEntity.prototype, "tokens_used", void 0);
__decorate([
    (0, typeorm_1.Column)({
        type: 'int',
        nullable: true,
        name: 'execution_time_ms',
    }),
    __metadata("design:type", Object)
], TaskAIContextEntity.prototype, "execution_time_ms", void 0);
__decorate([
    (0, typeorm_1.CreateDateColumn)({
        name: 'created_at',
    }),
    __metadata("design:type", Date)
], TaskAIContextEntity.prototype, "created_at", void 0);
exports.TaskAIContextEntity = TaskAIContextEntity = __decorate([
    (0, typeorm_1.Entity)('task_ai_contexts'),
    (0, typeorm_1.Index)(['task_id', 'execution_timestamp']) // Composite index for task history queries
    ,
    (0, typeorm_1.Index)(['ai_agent_type', 'context_type']) // Index for agent-specific queries
    ,
    (0, typeorm_1.Index)(['execution_timestamp']) // Index for time-based queries
], TaskAIContextEntity);
//# sourceMappingURL=task-ai-context.entity.js.map