'use strict';

module.exports = {
  up: async (queryInterface, Sequelize) => {
    // 创建设备表
    await queryInterface.createTable('devices', {
      id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true,
        comment: '设备ID'
      },
      device_code: {
        type: Sequelize.STRING(50),
        allowNull: false,
        unique: true,
        comment: '设备编号'
      },
      device_name: {
        type: Sequelize.STRING(100),
        allowNull: false,
        comment: '设备名称'
      },
      device_type: {
        type: Sequelize.STRING(50),
        allowNull: false,
        comment: '设备类型'
      },
      device_model: {
        type: Sequelize.STRING(100),
        comment: '设备型号'
      },
      manufacturer: {
        type: Sequelize.STRING(100),
        comment: '制造商'
      },
      installation_location: {
        type: Sequelize.STRING(200),
        comment: '安装位置'
      },
      installation_date: {
        type: Sequelize.DATE,
        comment: '安装日期'
      },
      status: {
        type: Sequelize.ENUM('normal', 'warning', 'error', 'offline'),
        defaultValue: 'normal',
        comment: '设备状态'
      },
      farm_id: {
        type: Sequelize.INTEGER,
        comment: '所属养殖场ID'
      },
      pen_id: {
        type: Sequelize.INTEGER,
        comment: '所属栏舍ID'
      },
      created_by: {
        type: Sequelize.INTEGER,
        comment: '创建人ID'
      },
      updated_by: {
        type: Sequelize.INTEGER,
        comment: '更新人ID'
      },
      created_at: {
        type: Sequelize.DATE,
        defaultValue: Sequelize.NOW,
        comment: '创建时间'
      },
      updated_at: {
        type: Sequelize.DATE,
        defaultValue: Sequelize.NOW,
        comment: '更新时间'
      }
    }, {
      comment: '设备信息表'
    });

    // 创建设备预警表
    await queryInterface.createTable('device_alerts', {
      id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true,
        comment: '预警ID'
      },
      device_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        comment: '设备ID',
        references: {
          model: 'devices',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'CASCADE'
      },
      alert_type: {
        type: Sequelize.STRING(50),
        allowNull: false,
        comment: '预警类型'
      },
      alert_level: {
        type: Sequelize.ENUM('low', 'medium', 'high', 'critical'),
        allowNull: false,
        comment: '预警级别'
      },
      alert_title: {
        type: Sequelize.STRING(200),
        allowNull: false,
        comment: '预警标题'
      },
      alert_content: {
        type: Sequelize.TEXT,
        comment: '预警内容'
      },
      alert_time: {
        type: Sequelize.DATE,
        allowNull: false,
        defaultValue: Sequelize.NOW,
        comment: '预警时间'
      },
      status: {
        type: Sequelize.ENUM('pending', 'processing', 'resolved', 'ignored'),
        defaultValue: 'pending',
        comment: '处理状态'
      },
      handler_id: {
        type: Sequelize.INTEGER,
        comment: '处理人ID',
        references: {
          model: 'users',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL'
      },
      handle_time: {
        type: Sequelize.DATE,
        comment: '处理时间'
      },
      handle_remark: {
        type: Sequelize.TEXT,
        comment: '处理备注'
      },
      farm_id: {
        type: Sequelize.INTEGER,
        comment: '所属养殖场ID'
      },
      pen_id: {
        type: Sequelize.INTEGER,
        comment: '所属栏舍ID'
      },
      is_read: {
        type: Sequelize.BOOLEAN,
        defaultValue: false,
        comment: '是否已读'
      },
      read_time: {
        type: Sequelize.DATE,
        comment: '阅读时间'
      },
      created_at: {
        type: Sequelize.DATE,
        defaultValue: Sequelize.NOW,
        comment: '创建时间'
      },
      updated_at: {
        type: Sequelize.DATE,
        defaultValue: Sequelize.NOW,
        comment: '更新时间'
      }
    }, {
      comment: '设备预警表'
    });

    // 添加索引
    await queryInterface.addIndex('device_alerts', ['device_id']);
    await queryInterface.addIndex('device_alerts', ['alert_level']);
    await queryInterface.addIndex('device_alerts', ['status']);
    await queryInterface.addIndex('device_alerts', ['alert_time']);
    await queryInterface.addIndex('device_alerts', ['farm_id']);
  },

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