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

const LivestockType = sequelize.define('LivestockType', {
  id: {
    type: DataTypes.INTEGER,
    autoIncrement: true,
    primaryKey: true,
    comment: '牲畜类型ID'
  },
  name: {
    type: DataTypes.STRING(50),
    allowNull: false,
    unique: true,
    comment: '牲畜类型名称',
    validate: {
      notEmpty: {
        msg: '牲畜类型名称不能为空'
      }
    }
  },
  code: {
    type: DataTypes.STRING(20),
    allowNull: false,
    unique: true,
    comment: '牲畜类型代码'
  },
  description: {
    type: DataTypes.TEXT,
    allowNull: true,
    comment: '牲畜类型描述'
  },
  unit_price_min: {
    type: DataTypes.DECIMAL(10, 2),
    allowNull: false,
    defaultValue: 0.00,
    comment: '单头最低价值',
    validate: {
      min: {
        args: [0],
        msg: '单头最低价值不能小于0'
      }
    }
  },
  unit_price_max: {
    type: DataTypes.DECIMAL(10, 2),
    allowNull: false,
    defaultValue: 50000.00,
    comment: '单头最高价值',
    validate: {
      min: {
        args: [0],
        msg: '单头最高价值不能小于0'
      }
    }
  },
  premium_rate: {
    type: DataTypes.DECIMAL(5, 4),
    allowNull: false,
    defaultValue: 0.0050,
    comment: '保险费率',
    validate: {
      min: {
        args: [0],
        msg: '保费费率不能小于0'
      },
      max: {
        args: [1],
        msg: '保费费率不能大于1'
      }
    }
  },
  status: {
    type: DataTypes.ENUM('active', 'inactive'),
    allowNull: false,
    defaultValue: 'active',
    comment: '状态'
  }
}, {
  tableName: 'livestock_types',
  timestamps: true,
  createdAt: 'created_at',
  updatedAt: 'updated_at',
  indexes: [
    {
      name: 'idx_livestock_type_name',
      fields: ['name']
    },
    {
      name: 'idx_livestock_type_code',
      fields: ['code']
    },
    {
      name: 'idx_livestock_type_status',
      fields: ['status']
    }
  ],
  comment: '牲畜类型表'
});

module.exports = LivestockType;

