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

const LivestockClaim = sequelize.define('LivestockClaim', {
  id: {
    type: DataTypes.INTEGER,
    autoIncrement: true,
    primaryKey: true,
    comment: '生资理赔ID'
  },
  claim_no: {
    type: DataTypes.STRING(50),
    allowNull: false,
    unique: true,
    comment: '理赔编号',
    validate: {
      notEmpty: {
        msg: '理赔编号不能为空'
      }
    }
  },
  reporter_name: {
    type: DataTypes.STRING(100),
    allowNull: false,
    comment: '报案人姓名'
  },
  contact_phone: {
    type: DataTypes.STRING(20),
    allowNull: false,
    comment: '联系电话'
  },
  policy_no: {
    type: DataTypes.STRING(50),
    allowNull: false,
    comment: '保单号'
  },
  policy_id: {
    type: DataTypes.INTEGER,
    allowNull: false,
    comment: '关联的保单ID',
    references: {
      model: 'livestock_policies',
      key: 'id'
    }
  },
  claim_type: {
    type: DataTypes.ENUM('death', 'disease', 'accident', 'natural_disaster'),
    allowNull: false,
    comment: '理赔类型：death-死亡，disease-疾病，accident-意外事故，natural_disaster-自然灾害'
  },
  affected_count: {
    type: DataTypes.INTEGER,
    allowNull: false,
    defaultValue: 1,
    comment: '受影响牲畜数量',
    validate: {
      min: {
        args: [1],
        msg: '受影响牲畜数量不能小于1'
      }
    }
  },
  claim_amount: {
    type: DataTypes.DECIMAL(15, 2),
    allowNull: false,
    comment: '理赔金额',
    validate: {
      min: {
        args: [0],
        msg: '理赔金额不能小于0'
      }
    }
  },
  incident_date: {
    type: DataTypes.DATE,
    allowNull: false,
    comment: '事故发生日期'
  },
  report_date: {
    type: DataTypes.DATE,
    allowNull: false,
    comment: '报案日期'
  },
  incident_description: {
    type: DataTypes.TEXT,
    allowNull: false,
    comment: '事故描述',
    validate: {
      notEmpty: {
        msg: '事故描述不能为空'
      }
    }
  },
  incident_location: {
    type: DataTypes.STRING(500),
    allowNull: false,
    comment: '事故地点',
    validate: {
      notEmpty: {
        msg: '事故地点不能为空'
      }
    }
  },
  claim_status: {
    type: DataTypes.ENUM('pending', 'investigating', 'approved', 'rejected', 'paid'),
    allowNull: false,
    defaultValue: 'pending',
    comment: '理赔状态：pending-待审核，investigating-调查中，approved-已批准，rejected-已拒绝，paid-已支付'
  },
  investigator_id: {
    type: DataTypes.INTEGER,
    allowNull: true,
    comment: '调查员ID',
    references: {
      model: 'users',
      key: 'id'
    }
  },
  investigation_report: {
    type: DataTypes.TEXT,
    allowNull: true,
    comment: '调查报告'
  },
  reviewer_id: {
    type: DataTypes.INTEGER,
    allowNull: true,
    comment: '审核人ID',
    references: {
      model: 'users',
      key: 'id'
    }
  },
  review_notes: {
    type: DataTypes.TEXT,
    allowNull: true,
    comment: '审核备注'
  },
  review_date: {
    type: DataTypes.DATE,
    allowNull: true,
    comment: '审核日期'
  },
  payment_date: {
    type: DataTypes.DATE,
    allowNull: true,
    comment: '赔付日期'
  },
  payment_amount: {
    type: DataTypes.DECIMAL(15, 2),
    allowNull: true,
    comment: '实际赔付金额',
    validate: {
      min: {
        args: [0],
        msg: '实际赔付金额不能小于0'
      }
    }
  },
  supporting_documents: {
    type: DataTypes.JSON,
    allowNull: true,
    comment: '支持文件（JSON数组，包含文件URL和描述）'
  },
  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_claims',
  timestamps: true,
  createdAt: 'created_at',
  updatedAt: 'updated_at',
  indexes: [
    {
      name: 'idx_livestock_claim_no',
      fields: ['claim_no'],
      unique: true
    },
    {
      name: 'idx_livestock_claim_policy',
      fields: ['policy_id']
    },
    {
      name: 'idx_livestock_claim_type',
      fields: ['claim_type']
    },
    {
      name: 'idx_livestock_claim_status',
      fields: ['claim_status']
    },
    {
      name: 'idx_livestock_claim_dates',
      fields: ['incident_date', 'report_date']
    }
  ],
  comment: '生资理赔表'
});

module.exports = LivestockClaim;