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

const LivestockPolicy = sequelize.define('LivestockPolicy', {
  id: {
    type: DataTypes.INTEGER,
    autoIncrement: true,
    primaryKey: true,
    comment: '生资保单ID'
  },
  policy_no: {
    type: DataTypes.STRING(50),
    allowNull: false,
    unique: true,
    comment: '保单编号',
    validate: {
      notEmpty: {
        msg: '保单编号不能为空'
      }
    }
  },
  livestock_type_id: {
    type: DataTypes.INTEGER,
    allowNull: false,
    comment: '牲畜类型ID',
    references: {
      model: 'livestock_types',
      key: 'id'
    }
  },
  policyholder_name: {
    type: DataTypes.STRING(100),
    allowNull: false,
    comment: '投保人姓名',
    validate: {
      notEmpty: {
        msg: '投保人姓名不能为空'
      }
    }
  },
  policyholder_id_card: {
    type: DataTypes.STRING(18),
    allowNull: false,
    comment: '投保人身份证号',
    validate: {
      notEmpty: {
        msg: '投保人身份证号不能为空'
      }
    }
  },
  policyholder_phone: {
    type: DataTypes.STRING(20),
    allowNull: false,
    comment: '投保人手机号',
    validate: {
      notEmpty: {
        msg: '投保人手机号不能为空'
      }
    }
  },
  policyholder_address: {
    type: DataTypes.STRING(500),
    allowNull: false,
    comment: '投保人地址',
    validate: {
      notEmpty: {
        msg: '投保人地址不能为空'
      }
    }
  },
  farm_name: {
    type: DataTypes.STRING(200),
    allowNull: true,
    comment: '养殖场名称'
  },
  farm_address: {
    type: DataTypes.STRING(500),
    allowNull: true,
    comment: '养殖场地址'
  },
  farm_license: {
    type: DataTypes.STRING(100),
    allowNull: true,
    comment: '养殖场许可证号'
  },
  livestock_count: {
    type: DataTypes.INTEGER,
    allowNull: false,
    comment: '投保牲畜数量',
    validate: {
      min: {
        args: [1],
        msg: '投保牲畜数量不能小于1'
      }
    }
  },
  unit_value: {
    type: DataTypes.DECIMAL(10, 2),
    allowNull: false,
    comment: '单头价值',
    validate: {
      min: {
        args: [0],
        msg: '单头价值不能小于0'
      }
    }
  },
  total_value: {
    type: DataTypes.DECIMAL(15, 2),
    allowNull: false,
    comment: '总保额',
    validate: {
      min: {
        args: [0],
        msg: '总保额不能小于0'
      }
    }
  },

  premium_amount: {
    type: DataTypes.DECIMAL(15, 2),
    allowNull: false,
    comment: '保费金额',
    validate: {
      min: {
        args: [0],
        msg: '保费金额不能小于0'
      }
    }
  },
  start_date: {
    type: DataTypes.DATE,
    allowNull: false,
    comment: '保险开始日期'
  },
  end_date: {
    type: DataTypes.DATE,
    allowNull: false,
    comment: '保险结束日期'
  },
  policy_status: {
    type: DataTypes.ENUM('active', 'expired', 'cancelled', 'suspended'),
    allowNull: false,
    defaultValue: 'active',
    comment: '保单状态：active-有效，expired-已过期，cancelled-已取消，suspended-已暂停'
  },
  payment_status: {
    type: DataTypes.ENUM('paid', 'unpaid', 'partial'),
    allowNull: false,
    defaultValue: 'unpaid',
    comment: '支付状态：paid-已支付，unpaid-未支付，partial-部分支付'
  },
  payment_date: {
    type: DataTypes.DATE,
    allowNull: true,
    comment: '支付日期'
  },
  policy_document_url: {
    type: DataTypes.STRING(500),
    allowNull: true,
    comment: '保单文档URL'
  },
  notes: {
    type: DataTypes.TEXT,
    allowNull: true,
    comment: '备注信息'
  },
  created_by: {
    type: DataTypes.INTEGER,
    allowNull: true,
    comment: '创建人ID',
    references: {
      model: 'users',
      key: 'id'
    }
  },
  updated_by: {
    type: DataTypes.INTEGER,
    allowNull: true,
    comment: '更新人ID',
    references: {
      model: 'users',
      key: 'id'
    }
  }
}, {
  tableName: 'livestock_policies',
  timestamps: true,
  createdAt: 'created_at',
  updatedAt: 'updated_at',
  indexes: [
    {
      name: 'idx_livestock_policy_no',
      fields: ['policy_no'],
      unique: true
    },
    {
      name: 'idx_livestock_policy_policyholder',
      fields: ['policyholder_name']
    },
    {
      name: 'idx_livestock_policy_status',
      fields: ['policy_status']
    },
    {
      name: 'idx_livestock_policy_payment_status',
      fields: ['payment_status']
    },
    {
      name: 'idx_livestock_policy_dates',
      fields: ['start_date', 'end_date']
    }
  ],
  comment: '生资保单表'
});

module.exports = LivestockPolicy;

