'use strict';

module.exports = {
  up: async (queryInterface, Sequelize) => {
    await queryInterface.createTable('cattle_exit_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'
      },
      exit_date: {
        type: Sequelize.DATE,
        allowNull: false,
        comment: '离栏日期'
      },
      exit_reason: {
        type: Sequelize.ENUM('出售', '死亡', '淘汰', '转场', '其他'),
        allowNull: false,
        comment: '离栏原因'
      },
      original_pen_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'cattle_pens',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'CASCADE',
        comment: '原栏舍ID'
      },
      destination: {
        type: Sequelize.STRING(200),
        allowNull: false,
        comment: '去向'
      },
      disposal_method: {
        type: Sequelize.ENUM('屠宰', '转售', '掩埋', '焚烧', '其他'),
        allowNull: false,
        comment: '处理方式'
      },
      handler: {
        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_exit_records', ['record_id'], {
      name: 'idx_cattle_exit_records_record_id',
      unique: true
    });

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

    await queryInterface.addIndex('cattle_exit_records', ['original_pen_id'], {
      name: 'idx_cattle_exit_records_original_pen_id'
    });

    await queryInterface.addIndex('cattle_exit_records', ['exit_date'], {
      name: 'idx_cattle_exit_records_exit_date'
    });

    await queryInterface.addIndex('cattle_exit_records', ['exit_reason'], {
      name: 'idx_cattle_exit_records_exit_reason'
    });

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

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