'use strict';

module.exports = {
  up: async (queryInterface, Sequelize) => {
    await queryInterface.createTable('cattle_transfer_records', {
      id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true,
        allowNull: false
      },
      record_id: {
        type: Sequelize.STRING(50),
        allowNull: false,
        unique: true,
        comment: '记录编号'
      },
      animal_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'animals',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'CASCADE',
        comment: '动物ID'
      },
      from_pen_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'cattle_pens',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'CASCADE',
        comment: '转出栏舍ID'
      },
      to_pen_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'cattle_pens',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'CASCADE',
        comment: '转入栏舍ID'
      },
      transfer_date: {
        type: Sequelize.DATE,
        allowNull: false,
        comment: '转栏日期'
      },
      reason: {
        type: Sequelize.ENUM('正常调栏', '疾病治疗', '配种需要', '产房准备', '隔离观察', '其他'),
        allowNull: false,
        comment: '转栏原因'
      },
      operator: {
        type: Sequelize.STRING(100),
        allowNull: false,
        comment: '操作人员'
      },
      status: {
        type: Sequelize.ENUM('已完成', '进行中'),
        allowNull: false,
        defaultValue: '已完成',
        comment: '状态'
      },
      remark: {
        type: Sequelize.TEXT,
        allowNull: true,
        comment: '备注'
      },
      farm_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'farms',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL',
        comment: '所属农场ID'
      },
      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')
      }
    }, {
      engine: 'InnoDB',
      charset: 'utf8mb4',
      collate: 'utf8mb4_unicode_ci',
      comment: '转栏记录表'
    });

    // 添加索引
    await queryInterface.addIndex('cattle_transfer_records', ['record_id'], {
      name: 'idx_cattle_transfer_records_record_id',
      unique: true
    });

    await queryInterface.addIndex('cattle_transfer_records', ['animal_id'], {
      name: 'idx_cattle_transfer_records_animal_id'
    });

    await queryInterface.addIndex('cattle_transfer_records', ['from_pen_id'], {
      name: 'idx_cattle_transfer_records_from_pen_id'
    });

    await queryInterface.addIndex('cattle_transfer_records', ['to_pen_id'], {
      name: 'idx_cattle_transfer_records_to_pen_id'
    });

    await queryInterface.addIndex('cattle_transfer_records', ['transfer_date'], {
      name: 'idx_cattle_transfer_records_transfer_date'
    });

    await queryInterface.addIndex('cattle_transfer_records', ['status'], {
      name: 'idx_cattle_transfer_records_status'
    });
  },

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