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

const CattleTransferRecord = sequelize.define('CattleTransferRecord', {
  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'
  },
  fromPenId: {
    type: DataTypes.INTEGER,
    allowNull: false,
    field: 'from_pen_id',
    comment: '转出栏舍ID'
  },
  toPenId: {
    type: DataTypes.INTEGER,
    allowNull: false,
    field: 'to_pen_id',
    comment: '转入栏舍ID'
  },
  transferDate: {
    type: DataTypes.DATE,
    allowNull: false,
    field: 'transfer_date',
    comment: '转栏日期'
  },
  reason: {
    type: DataTypes.ENUM('正常调栏', '疾病治疗', '配种需要', '产房准备', '隔离观察', '其他'),
    allowNull: false,
    comment: '转栏原因'
  },
  operator: {
    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_transfer_records',
  timestamps: true,
  createdAt: 'created_at',
  updatedAt: 'updated_at',
  comment: '转栏记录表'
});

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

//   // 转栏记录属于转出栏舍
//   CattleTransferRecord.belongsTo(models.CattlePen, {
//     foreignKey: 'fromPenId',
//     as: 'fromPen'
//   });

//   // 转栏记录属于转入栏舍
//   CattleTransferRecord.belongsTo(models.CattlePen, {
//     foreignKey: 'toPenId',
//     as: 'toPen'
//   });

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

module.exports = CattleTransferRecord;
