'use strict';

module.exports = {
  async up(queryInterface, Sequelize) {
    await queryInterface.createTable('reports', {
      id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true,
        allowNull: false
      },
      name: {
        type: Sequelize.STRING(255),
        allowNull: false,
        comment: '报表名称'
      },
      type: {
        type: Sequelize.ENUM('transaction', 'account', 'user'),
        allowNull: false,
        comment: '报表类型'
      },
      format: {
        type: Sequelize.ENUM('excel', 'pdf', 'csv'),
        allowNull: false,
        comment: '报表格式'
      },
      status: {
        type: Sequelize.ENUM('processing', 'completed', 'failed'),
        allowNull: false,
        defaultValue: 'processing',
        comment: '报表状态'
      },
      filePath: {
        type: Sequelize.STRING(500),
        allowNull: true,
        comment: '文件路径'
      },
      parameters: {
        type: Sequelize.JSON,
        allowNull: true,
        comment: '生成参数'
      },
      data: {
        type: Sequelize.JSON,
        allowNull: true,
        comment: '报表数据'
      },
      error: {
        type: Sequelize.TEXT,
        allowNull: true,
        comment: '错误信息'
      },
      createdBy: {
        type: Sequelize.INTEGER,
        allowNull: false,
        comment: '创建人ID',
        references: {
          model: 'bank_users',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'CASCADE'
      },
      createdAt: {
        type: Sequelize.DATE,
        allowNull: false,
        defaultValue: Sequelize.NOW
      },
      updatedAt: {
        type: Sequelize.DATE,
        allowNull: false,
        defaultValue: Sequelize.NOW
      }
    });

    // 添加索引
    await queryInterface.addIndex('reports', ['type', 'status']);
    await queryInterface.addIndex('reports', ['createdBy']);
    await queryInterface.addIndex('reports', ['createdAt']);
  },

  async down(queryInterface, Sequelize) {
    await queryInterface.dropTable('reports');
  }
};
