const { DataTypes } = require('sequelize');
const { sequelize } = require('../config/database-simple');

const CattleExitRecord = sequelize.define('CattleExitRecord', {
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true,
    allowNull: false
  },
  recordId: {
    type: DataTypes.STRING(50),
    allowNull: false,
    unique: true,
    field: 'record_id',
    comment: '记录编号'
  },
  animalId: {
    type: DataTypes.INTEGER,
    allowNull: false,
    field: 'animal_id',
    comment: '动物ID'
  },
  exitDate: {
    type: DataTypes.DATE,
    allowNull: false,
    field: 'exit_date',
    comment: '离栏日期'
  },
  exitReason: {
    type: DataTypes.ENUM('出售', '死亡', '淘汰', '转场', '其他'),
    allowNull: false,
    field: 'exit_reason',
    comment: '离栏原因'
  },
  originalPenId: {
    type: DataTypes.INTEGER,
    allowNull: false,
    field: 'original_pen_id',
    comment: '原栏舍ID'
  },
  destination: {
    type: DataTypes.STRING(200),
    allowNull: false,
    comment: '去向'
  },
  disposalMethod: {
    type: DataTypes.ENUM('屠宰', '转售', '掩埋', '焚烧', '其他'),
    allowNull: false,
    field: 'disposal_method',
    comment: '处理方式'
  },
  handler: {
    type: DataTypes.STRING(100),
    allowNull: false,
    comment: '处理人员'
  },
  status: {
    type: DataTypes.ENUM('已确认', '待确认', '已取消'),
    allowNull: false,
    defaultValue: '待确认',
    comment: '状态'
  },
  remark: {
    type: DataTypes.TEXT,
    allowNull: true,
    comment: '备注'
  },
  farmId: {
    type: DataTypes.INTEGER,
    allowNull: true,
    field: 'farm_id',
    comment: '所属农场ID'
  },
  earNumber: {
    type: DataTypes.STRING(50),
    allowNull: true,
    field: 'ear_number',
    comment: '牛只耳号'
  }
}, {
  tableName: 'cattle_exit_records',
  timestamps: true,
  createdAt: 'created_at',
  updatedAt: 'updated_at',
  comment: '离栏记录表'
});

// 关联关系已在 models/index.js 中定义
// CattleExitRecord.associate = (models) => {
//   // 离栏记录属于牛只
//   CattleExitRecord.belongsTo(models.IotCattle, {
//     foreignKey: 'animalId',
//     as: 'cattle'
//   });

//   // 离栏记录属于原栏舍
//   CattleExitRecord.belongsTo(models.CattlePen, {
//     foreignKey: 'originalPenId',
//     as: 'originalPen'
//   });

//   // 离栏记录属于农场
//   CattleExitRecord.belongsTo(models.Farm, {
//     foreignKey: 'farmId',
//     as: 'farm'
//   });
// };

module.exports = CattleExitRecord;
