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

const CattleBatch = sequelize.define('CattleBatch', {
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true,
    allowNull: false
  },
  name: {
    type: DataTypes.STRING(200),
    allowNull: false,
    comment: '批次名称'
  },
  code: {
    type: DataTypes.STRING(50),
    allowNull: false,
    unique: true,
    comment: '批次编号'
  },
  type: {
    type: DataTypes.ENUM('育成批次', '繁殖批次', '育肥批次', '隔离批次', '治疗批次'),
    allowNull: false,
    comment: '批次类型'
  },
  startDate: {
    type: DataTypes.DATE,
    allowNull: false,
    field: 'start_date',
    comment: '开始日期'
  },
  expectedEndDate: {
    type: DataTypes.DATE,
    allowNull: true,
    field: 'expected_end_date',
    comment: '预计结束日期'
  },
  actualEndDate: {
    type: DataTypes.DATE,
    allowNull: true,
    field: 'actual_end_date',
    comment: '实际结束日期'
  },
  targetCount: {
    type: DataTypes.INTEGER,
    allowNull: false,
    field: 'target_count',
    comment: '目标牛只数量'
  },
  currentCount: {
    type: DataTypes.INTEGER,
    allowNull: false,
    defaultValue: 0,
    field: 'current_count',
    comment: '当前牛只数量'
  },
  manager: {
    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'
  }
}, {
  tableName: 'cattle_batches',
  timestamps: true,
  createdAt: 'created_at',
  updatedAt: 'updated_at',
  comment: '批次设置表'
});

// 关联关系已在 models/index.js 中定义
// CattleBatch.associate = (models) => {
//   // 批次属于农场
//   CattleBatch.belongsTo(models.Farm, {
//     foreignKey: 'farmId',
//     as: 'farm'
//   });

//   // 批次与动物的多对多关系
//   CattleBatch.belongsToMany(models.Animal, {
//     through: 'cattle_batch_animals',
//     foreignKey: 'batch_id',
//     otherKey: 'animal_id',
//     as: 'animals'
//   });
// };

module.exports = CattleBatch;
