"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CreateTaskAIContextsTable1736662000000 = void 0;
const typeorm_1 = require("typeorm");
/**
 * Migration to create task_ai_contexts table
 * Based on architecture.md#task_ai_contexts Collection (lines 213-224)
 */
class CreateTaskAIContextsTable1736662000000 {
    constructor() {
        this.name = 'CreateTaskAIContextsTable1736662000000';
    }
    async up(queryRunner) {
        // Create the task_ai_contexts table
        await queryRunner.createTable(new typeorm_1.Table({
            name: 'task_ai_contexts',
            columns: [
                {
                    name: 'id',
                    type: 'varchar',
                    isPrimary: true,
                    generationStrategy: 'uuid',
                    default: `(lower(hex(randomblob(4))) || '-' || lower(hex(randomblob(2))) || '-4' || substr(lower(hex(randomblob(2))),2) || '-' || substr('89ab',abs(random()) % 4 + 1, 1) || substr(lower(hex(randomblob(2))),2) || '-' || lower(hex(randomblob(6))))`,
                },
                {
                    name: 'task_id',
                    type: 'varchar',
                    isNullable: false,
                    comment: 'Foreign key to tasks.id',
                },
                {
                    name: 'ai_agent_type',
                    type: 'varchar',
                    length: '50',
                    isNullable: false,
                    comment: 'Type of AI agent (openai, langchain, claude_code, bmad_agent, etc.)',
                },
                {
                    name: 'context_type',
                    type: 'varchar',
                    length: '20',
                    isNullable: false,
                    comment: 'Type of context (prompt, result, feedback, error)',
                },
                {
                    name: 'context_data',
                    type: 'text',
                    isNullable: true,
                    comment: 'JSON data containing prompts, responses, metadata',
                },
                {
                    name: 'execution_timestamp',
                    type: 'datetime',
                    isNullable: false,
                    comment: 'Timestamp when the AI execution occurred',
                },
                {
                    name: 'success',
                    type: 'boolean',
                    default: true,
                    isNullable: false,
                    comment: 'Whether the AI operation was successful',
                },
                {
                    name: 'tokens_used',
                    type: 'integer',
                    isNullable: true,
                    comment: 'Number of tokens used in the operation',
                },
                {
                    name: 'execution_time_ms',
                    type: 'integer',
                    isNullable: true,
                    comment: 'Execution time in milliseconds',
                },
                {
                    name: 'created_at',
                    type: 'datetime',
                    default: 'CURRENT_TIMESTAMP',
                    isNullable: false,
                    comment: 'Timestamp when the record was created',
                },
            ],
        }), true);
        // Create indexes for efficient querying
        await queryRunner.createIndex('task_ai_contexts', new typeorm_1.Index({
            name: 'IDX_task_ai_contexts_task_id',
            columnNames: ['task_id'],
        }));
        await queryRunner.createIndex('task_ai_contexts', new typeorm_1.Index({
            name: 'IDX_task_ai_contexts_task_execution',
            columnNames: ['task_id', 'execution_timestamp'],
        }));
        await queryRunner.createIndex('task_ai_contexts', new typeorm_1.Index({
            name: 'IDX_task_ai_contexts_agent_type',
            columnNames: ['ai_agent_type', 'context_type'],
        }));
        await queryRunner.createIndex('task_ai_contexts', new typeorm_1.Index({
            name: 'IDX_task_ai_contexts_timestamp',
            columnNames: ['execution_timestamp'],
        }));
        // Add check constraints for enum values
        await queryRunner.query(`
      CREATE TRIGGER task_ai_contexts_context_type_check
      BEFORE INSERT ON task_ai_contexts
      FOR EACH ROW
      BEGIN
        SELECT CASE
          WHEN NEW.context_type NOT IN ('prompt', 'result', 'feedback', 'error')
          THEN RAISE(ABORT, 'Invalid context_type value')
        END;
      END;
    `);
        await queryRunner.query(`
      CREATE TRIGGER task_ai_contexts_context_type_update_check
      BEFORE UPDATE ON task_ai_contexts
      FOR EACH ROW
      BEGIN
        SELECT CASE
          WHEN NEW.context_type NOT IN ('prompt', 'result', 'feedback', 'error')
          THEN RAISE(ABORT, 'Invalid context_type value')
        END;
      END;
    `);
        // Add check constraints for non-negative values
        await queryRunner.query(`
      CREATE TRIGGER task_ai_contexts_tokens_check
      BEFORE INSERT ON task_ai_contexts
      FOR EACH ROW
      BEGIN
        SELECT CASE
          WHEN NEW.tokens_used IS NOT NULL AND NEW.tokens_used < 0
          THEN RAISE(ABORT, 'tokens_used must be non-negative')
        END;
      END;
    `);
        await queryRunner.query(`
      CREATE TRIGGER task_ai_contexts_execution_time_check
      BEFORE INSERT ON task_ai_contexts
      FOR EACH ROW
      BEGIN
        SELECT CASE
          WHEN NEW.execution_time_ms IS NOT NULL AND NEW.execution_time_ms < 0
          THEN RAISE(ABORT, 'execution_time_ms must be non-negative')
        END;
      END;
    `);
    }
    async down(queryRunner) {
        // Drop triggers
        await queryRunner.query('DROP TRIGGER IF EXISTS task_ai_contexts_context_type_check');
        await queryRunner.query('DROP TRIGGER IF EXISTS task_ai_contexts_context_type_update_check');
        await queryRunner.query('DROP TRIGGER IF EXISTS task_ai_contexts_tokens_check');
        await queryRunner.query('DROP TRIGGER IF EXISTS task_ai_contexts_execution_time_check');
        // Drop indexes
        await queryRunner.dropIndex('task_ai_contexts', 'IDX_task_ai_contexts_task_id');
        await queryRunner.dropIndex('task_ai_contexts', 'IDX_task_ai_contexts_task_execution');
        await queryRunner.dropIndex('task_ai_contexts', 'IDX_task_ai_contexts_agent_type');
        await queryRunner.dropIndex('task_ai_contexts', 'IDX_task_ai_contexts_timestamp');
        // Drop table
        await queryRunner.dropTable('task_ai_contexts');
    }
}
exports.CreateTaskAIContextsTable1736662000000 = CreateTaskAIContextsTable1736662000000;
//# sourceMappingURL=create-task-ai-contexts-table.migration.js.map