/**
 * 动物信息模型
 */
const { DataTypes, Model } = require('sequelize');
const sequelize = require('../config/database');

class Animal extends Model {
  // 获取动物类型文本
  getAnimalTypeText() {
    const typeMap = {
      1: '牛',
      2: '羊',
      3: '猪',
      4: '马'
    };
    return typeMap[this.animal_type] || '未知';
  }

  // 获取品种文本
  getBreedText() {
    const breedMap = {
      1: '西藏高山牦牛',
      2: '荷斯坦奶牛',
      3: '西门塔尔牛',
      4: '安格斯牛',
      5: '小尾寒羊',
      6: '波尔山羊'
    };
    return breedMap[this.breed] || '未知品种';
  }

  // 获取品类文本
  getCategoryText() {
    const categoryMap = {
      1: '乳肉兼用',
      2: '肉用',
      3: '乳用',
      4: '种用'
    };
    return categoryMap[this.category] || '未知品类';
  }

  // 获取来源类型文本
  getSourceTypeText() {
    const sourceMap = {
      1: '合作社',
      2: '农户',
      3: '养殖场',
      4: '进口'
    };
    return sourceMap[this.source_type] || '未知来源';
  }

  // 格式化出生日期
  getBirthDateFormatted() {
    if (this.birth_date) {
      const date = new Date(this.birth_date);
      return date.toLocaleString('zh-CN', {
        year: 'numeric',
        month: '2-digit',
        day: '2-digit',
        hour: '2-digit',
        minute: '2-digit',
        second: '2-digit'
      }).replace(/\//g, '-');
    }
    return '';
  }

  // 格式化入场日期
  getEntryDateFormatted() {
    if (this.entry_date) {
      const date = new Date(this.entry_date);
      return date.toLocaleString('zh-CN', {
        year: 'numeric',
        month: '2-digit',
        day: '2-digit',
        hour: '2-digit',
        minute: '2-digit',
        second: '2-digit'
      }).replace(/\//g, '-');
    }
    return '';
  }
}

Animal.init({
  id: {
    type: DataTypes.BIGINT,
    primaryKey: true,
    autoIncrement: true,
    comment: '动物ID'
  },
  collar_number: {
    type: DataTypes.STRING(50),
    allowNull: false,
    comment: '项圈编号'
  },
  ear_tag: {
    type: DataTypes.STRING(50),
    allowNull: true,
    comment: '动物耳号'
  },
  animal_type: {
    type: DataTypes.INTEGER,
    allowNull: false,
    defaultValue: 1,
    comment: '动物类型：1-牛，2-羊，3-猪，4-马'
  },
  breed: {
    type: DataTypes.INTEGER,
    allowNull: false,
    defaultValue: 1,
    comment: '品种：1-西藏高山牦牛，2-荷斯坦奶牛，3-西门塔尔牛，4-安格斯牛，5-小尾寒羊，6-波尔山羊'
  },
  category: {
    type: DataTypes.INTEGER,
    allowNull: false,
    defaultValue: 1,
    comment: '品类：1-乳肉兼用，2-肉用，3-乳用，4-种用'
  },
  source_type: {
    type: DataTypes.INTEGER,
    allowNull: false,
    defaultValue: 1,
    comment: '来源类型：1-合作社，2-农户，3-养殖场，4-进口'
  },
  birth_date: {
    type: DataTypes.DATE,
    allowNull: true,
    comment: '出生日期'
  },
  birth_weight: {
    type: DataTypes.DECIMAL(10, 2),
    allowNull: true,
    defaultValue: 0.00,
    comment: '出生体重'
  },
  weaning_weight: {
    type: DataTypes.DECIMAL(10, 2),
    allowNull: true,
    defaultValue: 0.00,
    comment: '断奶体重'
  },
  weaning_age: {
    type: DataTypes.INTEGER,
    allowNull: true,
    defaultValue: 0,
    comment: '断奶日龄'
  },
  entry_date: {
    type: DataTypes.DATE,
    allowNull: true,
    comment: '入场日期'
  },
  calving_count: {
    type: DataTypes.INTEGER,
    allowNull: true,
    defaultValue: 0,
    comment: '历史已产胎次'
  },
  left_teat_count: {
    type: DataTypes.INTEGER,
    allowNull: true,
    comment: '乳头数(左)'
  },
  right_teat_count: {
    type: DataTypes.INTEGER,
    allowNull: true,
    comment: '乳头数(右)'
  },
  farm_id: {
    type: DataTypes.INTEGER,
    allowNull: true,
    comment: '农场ID'
  },
  status: {
    type: DataTypes.INTEGER,
    allowNull: false,
    defaultValue: 1,
    comment: '状态：1-正常，2-生病，3-死亡'
  },
  created_at: {
    type: DataTypes.DATE,
    allowNull: false,
    defaultValue: DataTypes.NOW,
    comment: '创建时间'
  },
  updated_at: {
    type: DataTypes.DATE,
    allowNull: false,
    defaultValue: DataTypes.NOW,
    comment: '更新时间'
  }
}, {
  sequelize,
  modelName: 'Animal',
  tableName: 'animals',
  timestamps: true,
  createdAt: 'created_at',
  updatedAt: 'updated_at',
  indexes: [
    {
      fields: ['collar_number']
    },
    {
      fields: ['ear_tag']
    },
    {
      fields: ['farm_id']
    }
  ]
});

module.exports = Animal;