'use strict';

module.exports = {
  up: async (queryInterface, Sequelize) => {
    await queryInterface.createTable('operation_logs', {
      id: {
        type: Sequelize.INTEGER,
        autoIncrement: true,
        primaryKey: true
      },
      user_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'users',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'CASCADE'
      },
      operation_type: {
        type: Sequelize.ENUM(
          'login',           // 登录
          'logout',          // 登出
          'create',          // 创建
          'update',          // 更新
          'delete',          // 删除
          'view',            // 查看
          'export',          // 导出
          'import',          // 导入
          'approve',         // 审批
          'reject',          // 拒绝
          'system_config',   // 系统配置
          'user_manage',     // 用户管理
          'role_manage',     // 角色管理
          'other'            // 其他
        ),
        allowNull: false,
        comment: '操作类型'
      },
      operation_module: {
        type: Sequelize.STRING(50),
        allowNull: false,
        comment: '操作模块（如：用户管理、设备管理、预警管理等）'
      },
      operation_content: {
        type: Sequelize.TEXT,
        allowNull: false,
        comment: '操作内容描述'
      },
      operation_target: {
        type: Sequelize.STRING(100),
        allowNull: true,
        comment: '操作目标（如：用户ID、设备ID等）'
      },
      request_method: {
        type: Sequelize.ENUM('GET', 'POST', 'PUT', 'DELETE', 'PATCH'),
        allowNull: true,
        comment: 'HTTP请求方法'
      },
      request_url: {
        type: Sequelize.STRING(500),
        allowNull: true,
        comment: '请求URL'
      },
      request_params: {
        type: Sequelize.TEXT,
        allowNull: true,
        comment: '请求参数（JSON格式）'
      },
      response_status: {
        type: Sequelize.INTEGER,
        allowNull: true,
        comment: '响应状态码'
      },
      ip_address: {
        type: Sequelize.STRING(45),
        allowNull: true,
        comment: 'IP地址（支持IPv6）'
      },
      user_agent: {
        type: Sequelize.TEXT,
        allowNull: true,
        comment: '用户代理信息'
      },
      execution_time: {
        type: Sequelize.INTEGER,
        allowNull: true,
        comment: '执行时间（毫秒）'
      },
      status: {
        type: Sequelize.ENUM('success', 'failed', 'error'),
        defaultValue: 'success',
        comment: '操作状态'
      },
      error_message: {
        type: Sequelize.TEXT,
        allowNull: true,
        comment: '错误信息'
      },
      created_at: {
        type: Sequelize.DATE,
        allowNull: false,
        defaultValue: Sequelize.literal('CURRENT_TIMESTAMP')
      },
      updated_at: {
        type: Sequelize.DATE,
        allowNull: false,
        defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP')
      }
    }, {
      charset: 'utf8mb4',
      collate: 'utf8mb4_unicode_ci',
      comment: '系统操作日志表'
    });

    // 创建索引
    await queryInterface.addIndex('operation_logs', ['user_id'], {
      name: 'idx_operation_logs_user_id'
    });
    
    await queryInterface.addIndex('operation_logs', ['operation_type'], {
      name: 'idx_operation_logs_operation_type'
    });
    
    await queryInterface.addIndex('operation_logs', ['operation_module'], {
      name: 'idx_operation_logs_operation_module'
    });
    
    await queryInterface.addIndex('operation_logs', ['created_at'], {
      name: 'idx_operation_logs_created_at'
    });
    
    await queryInterface.addIndex('operation_logs', ['status'], {
      name: 'idx_operation_logs_status'
    });
    
    await queryInterface.addIndex('operation_logs', ['ip_address'], {
      name: 'idx_operation_logs_ip_address'
    });
  },

  down: async (queryInterface, Sequelize) => {
    await queryInterface.dropTable('operation_logs');
  }
};