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

const Claim = sequelize.define('Claim', {
  id: {
    type: DataTypes.INTEGER,
    autoIncrement: true,
    primaryKey: true,
    comment: '理赔ID'
  },
  claim_no: {
    type: DataTypes.STRING(50),
    allowNull: false,
    unique: true,
    comment: '理赔编号',
    validate: {
      notEmpty: {
        msg: '理赔编号不能为空'
      }
    }
  },
  policy_id: {
    type: DataTypes.INTEGER,
    allowNull: false,
    comment: '关联的保单ID',
    references: {
      model: 'policies',
      key: 'id'
    }
  },
  customer_id: {
    type: DataTypes.INTEGER,
    allowNull: false,
    comment: '客户ID',
    references: {
      model: 'users',
      key: 'id'
    }
  },
  claim_amount: {
    type: DataTypes.DECIMAL(15, 2),
    allowNull: false,
    comment: '理赔金额',
    validate: {
      min: {
        args: [0],
        msg: '理赔金额不能小于0'
      }
    }
  },
  claim_date: {
    type: DataTypes.DATE,
    allowNull: false,
    comment: '理赔发生日期'
  },
  incident_description: {
    type: DataTypes.TEXT,
    allowNull: false,
    comment: '事故描述',
    validate: {
      notEmpty: {
        msg: '事故描述不能为空'
      }
    }
  },
  claim_status: {
    type: DataTypes.ENUM('pending', 'approved', 'rejected', 'processing', 'paid'),
    allowNull: false,
    defaultValue: 'pending',
    comment: '理赔状态：pending-待审核，approved-已批准，rejected-已拒绝，processing-处理中，paid-已支付'
  },
  review_notes: {
    type: DataTypes.TEXT,
    allowNull: true,
    comment: '审核备注'
  },
  reviewer_id: {
    type: DataTypes.INTEGER,
    allowNull: true,
    comment: '审核人ID',
    references: {
      model: 'users',
      key: 'id'
    }
  },
  review_date: {
    type: DataTypes.DATE,
    allowNull: true,
    comment: '审核日期'
  },
  payment_date: {
    type: DataTypes.DATE,
    allowNull: true,
    comment: '支付日期'
  },
  supporting_documents: {
    type: DataTypes.JSON,
    allowNull: true,
    comment: '支持文件（JSON数组，包含文件URL和描述）'
  },
  created_by: {
    type: DataTypes.INTEGER,
    allowNull: true,
    comment: '创建人ID'
  },
  updated_by: {
    type: DataTypes.INTEGER,
    allowNull: true,
    comment: '更新人ID'
  }
}, {
  tableName: 'claims',
  timestamps: true,
  createdAt: 'created_at',
  updatedAt: 'updated_at',
  indexes: [
    {
      name: 'idx_claim_no',
      fields: ['claim_no'],
      unique: true
    },
    {
      name: 'idx_claim_policy',
      fields: ['policy_id']
    },
    {
      name: 'idx_claim_customer',
      fields: ['customer_id']
    },
    {
      name: 'idx_claim_status',
      fields: ['claim_status']
    },
    {
      name: 'idx_claim_date',
      fields: ['claim_date']
    }
  ],
  comment: '理赔表'
});

module.exports = Claim;

