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

const Policy = sequelize.define('Policy', {
  id: {
    type: DataTypes.INTEGER,
    autoIncrement: true,
    primaryKey: true,
    comment: '保单ID'
  },
  policy_no: {
    type: DataTypes.STRING(50),
    allowNull: false,
    unique: true,
    comment: '保单编号',
    validate: {
      notEmpty: {
        msg: '保单编号不能为空'
      }
    }
  },
  application_id: {
    type: DataTypes.INTEGER,
    allowNull: false,
    comment: '关联的保险申请ID',
    references: {
      model: 'insurance_applications',
      key: 'id'
    }
  },
  insurance_type_id: {
    type: DataTypes.INTEGER,
    allowNull: false,
    comment: '保险类型ID',
    references: {
      model: 'insurance_types',
      key: 'id'
    }
  },
  customer_id: {
    type: DataTypes.INTEGER,
    allowNull: false,
    comment: '客户ID',
    references: {
      model: 'users',
      key: 'id'
    }
  },
  coverage_amount: {
    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: '保险结束日期'
  },
  policyholder_name: {
    type: DataTypes.STRING(100),
    allowNull: true,
    comment: '投保人姓名'
  },
  insured_name: {
    type: DataTypes.STRING(100),
    allowNull: true,
    comment: '被保险人姓名'
  },
  phone: {
    type: DataTypes.STRING(20),
    allowNull: true,
    comment: '联系电话'
  },
  email: {
    type: DataTypes.STRING(100),
    allowNull: true,
    comment: '电子邮箱'
  },
  address: {
    type: DataTypes.STRING(500),
    allowNull: true,
    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'
  },
  terms_and_conditions: {
    type: DataTypes.TEXT,
    allowNull: true,
    comment: '条款和条件'
  },
  created_by: {
    type: DataTypes.INTEGER,
    allowNull: true,
    comment: '创建人ID'
  },
  updated_by: {
    type: DataTypes.INTEGER,
    allowNull: true,
    comment: '更新人ID'
  }
}, {
  tableName: 'policies',
  timestamps: true,
  createdAt: 'created_at',
  updatedAt: 'updated_at',
  indexes: [
    {
      name: 'idx_policy_no',
      fields: ['policy_no'],
      unique: true
    },
    {
      name: 'idx_policy_customer',
      fields: ['customer_id']
    },
    {
      name: 'idx_policy_application',
      fields: ['application_id']
    },
    {
      name: 'idx_policy_status',
      fields: ['policy_status']
    },
    {
      name: 'idx_policy_payment_status',
      fields: ['payment_status']
    },
    {
      name: 'idx_policy_dates',
      fields: ['start_date', 'end_date']
    }
  ],
  comment: '保单表'
});

module.exports = Policy;