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

const InsuranceType = sequelize.define('InsuranceType', {
  id: {
    type: DataTypes.INTEGER,
    autoIncrement: true,
    primaryKey: true,
    comment: '保险类型ID'
  },
  name: {
    type: DataTypes.STRING(100),
    allowNull: false,
    comment: '险种名称',
    validate: {
      notEmpty: {
        msg: '险种名称不能为空'
      },
      len: {
        args: [1, 100],
        msg: '险种名称长度必须在1-100个字符之间'
      }
    }
  },


  description: {
    type: DataTypes.TEXT,
    allowNull: true,
    comment: '保险类型描述'
  },
  applicable_livestock: {
    type: DataTypes.STRING(100),
    allowNull: true,
    comment: '适用生资（如：牛、羊、猪等）'
  },
  insurance_term: {
    type: DataTypes.INTEGER,
    allowNull: true,
    comment: '保险期限（月）',
    validate: {
      min: {
        args: [1],
        msg: '保险期限不能小于1个月'
      }
    }
  },
  policy_form: {
    type: DataTypes.STRING(50),
    allowNull: true,
    comment: '保单形式（电子保单、纸质保单等）'
  },
  on_sale_status: {
    type: DataTypes.BOOLEAN,
    allowNull: false,
    defaultValue: true,
    comment: '在售状态（true-在售，false-停售）'
  },
  add_time: {
    type: DataTypes.DATE,
    allowNull: true,
    comment: '添加时间'
  },
  coverage_amount_min: {
    type: DataTypes.DECIMAL(15, 2),
    allowNull: false,
    defaultValue: 0.00,
    comment: '最低保额',
    validate: {
      min: {
        args: [0],
        msg: '最低保额不能小于0'
      }
    }
  },
  coverage_amount_max: {
    type: DataTypes.DECIMAL(15, 2),
    allowNull: false,
    defaultValue: 1000000.00,
    comment: '最高保额',
    validate: {
      min: {
        args: [0],
        msg: '最高保额不能小于0'
      }
    }
  },
  premium_rate: {
    type: DataTypes.DECIMAL(5, 4),
    allowNull: false,
    defaultValue: 0.001,
    comment: '保险费率',
    validate: {
      min: {
        args: [0],
        msg: '保险费率不能小于0'
      },
      max: {
        args: [1],
        msg: '保险费率不能大于1'
      }
    }
  },
  status: {
    type: DataTypes.ENUM('active', 'inactive'),
    allowNull: false,
    defaultValue: 'active',
    comment: '状态：active-启用，inactive-停用'
  },
  created_by: {
    type: DataTypes.INTEGER,
    allowNull: true,
    comment: '创建人ID'
  },
  updated_by: {
    type: DataTypes.INTEGER,
    allowNull: true,
    comment: '更新人ID'
  }
}, {
  tableName: 'insurance_types',
  timestamps: true,
  createdAt: 'created_at',
  updatedAt: 'updated_at',
  indexes: [
    {
      name: 'idx_insurance_type_name',
      fields: ['name'],
      unique: true
    },
    {
      name: 'idx_insurance_type_status',
      fields: ['status']
    }
  ],
  comment: '保险类型表'
});

module.exports = InsuranceType;