import { MigrationInterface, QueryRunner, Table } from 'typeorm';

export class AddAiChat1784780000000 implements MigrationInterface {
  async up(queryRunner: QueryRunner): Promise<void> {
    if (!(await queryRunner.hasTable('ai_conversations'))) {
      await queryRunner.createTable(
        new Table({
          name: 'ai_conversations',
          columns: [
            { name: 'id', type: 'integer', isPrimary: true, isGenerated: true, generationStrategy: 'increment' },
            { name: 'user_id', type: 'integer' },
            { name: 'title', type: 'varchar', length: '100', default: "'新对话'" },
            { name: 'created_at', type: 'datetime', default: 'CURRENT_TIMESTAMP' },
            { name: 'updated_at', type: 'datetime', default: 'CURRENT_TIMESTAMP' },
            { name: 'last_message_at', type: 'datetime', isNullable: true },
          ],
          indices: [
            { name: 'idx_ai_conversations_user_last_message', columnNames: ['user_id', 'last_message_at'] },
          ],
          foreignKeys: [
            { name: 'fk_ai_conversations_user', columnNames: ['user_id'], referencedTableName: 'users', referencedColumnNames: ['id'], onDelete: 'CASCADE' },
          ],
        }),
      );
    }

    if (!(await queryRunner.hasTable('ai_messages'))) {
      await queryRunner.createTable(
        new Table({
          name: 'ai_messages',
          columns: [
            { name: 'id', type: 'integer', isPrimary: true, isGenerated: true, generationStrategy: 'increment' },
            { name: 'conversation_id', type: 'integer' },
            { name: 'role', type: 'varchar', length: '20' },
            { name: 'content', type: 'text' },
            { name: 'reasoning_content', type: 'text', isNullable: true },
            { name: 'status', type: 'varchar', length: '20', default: "'completed'" },
            { name: 'error_code', type: 'varchar', length: '50', isNullable: true },
            { name: 'created_at', type: 'datetime', default: 'CURRENT_TIMESTAMP' },
            { name: 'updated_at', type: 'datetime', default: 'CURRENT_TIMESTAMP' },
          ],
          indices: [
            { name: 'idx_ai_messages_conversation_created', columnNames: ['conversation_id', 'created_at'] },
          ],
          foreignKeys: [
            { name: 'fk_ai_messages_conversation', columnNames: ['conversation_id'], referencedTableName: 'ai_conversations', referencedColumnNames: ['id'], onDelete: 'CASCADE' },
          ],
        }),
      );
    }

    if (!(await queryRunner.hasTable('ai_tool_runs'))) {
      await queryRunner.createTable(
        new Table({
          name: 'ai_tool_runs',
          columns: [
            { name: 'id', type: 'integer', isPrimary: true, isGenerated: true, generationStrategy: 'increment' },
            { name: 'message_id', type: 'integer' },
            { name: 'tool_call_id', type: 'varchar', length: '100' },
            { name: 'tool_name', type: 'varchar', length: '64' },
            { name: 'arguments_summary', type: 'text', isNullable: true },
            { name: 'result_summary', type: 'text', isNullable: true },
            { name: 'status', type: 'varchar', length: '20' },
            { name: 'duration_ms', type: 'integer', isNullable: true },
            { name: 'created_at', type: 'datetime', default: 'CURRENT_TIMESTAMP' },
          ],
          indices: [
            { name: 'idx_ai_tool_runs_message', columnNames: ['message_id'] },
          ],
          foreignKeys: [
            { name: 'fk_ai_tool_runs_message', columnNames: ['message_id'], referencedTableName: 'ai_messages', referencedColumnNames: ['id'], onDelete: 'CASCADE' },
          ],
        }),
      );
    }
  }

  async down(queryRunner: QueryRunner): Promise<void> {
    for (const table of ['ai_tool_runs', 'ai_messages', 'ai_conversations']) {
      if (await queryRunner.hasTable(table)) await queryRunner.dropTable(table);
    }
  }
}
