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

/**
 * 监管任务模型
 * 用于管理保险监管相关的任务数据
 */
const SupervisoryTask = sequelize.define('SupervisoryTask', {
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true,
    comment: '主键ID'
  },
  applicationNumber: {
    type: DataTypes.STRING(50),
    allowNull: false,
    unique: true,
    comment: '申请单号'
  },
  policyNumber: {
    type: DataTypes.STRING(50),
    allowNull: false,
    comment: '保单编号'
  },
  productName: {
    type: DataTypes.STRING(100),
    allowNull: false,
    comment: '产品名称'
  },
  insurancePeriod: {
    type: DataTypes.STRING(50),
    allowNull: false,
    comment: '保险周期'
  },
  customerName: {
    type: DataTypes.STRING(50),
    allowNull: false,
    comment: '客户姓名'
  },
  idType: {
    type: DataTypes.ENUM('身份证', '护照', '军官证', '士兵证', '港澳台居民居住证', '其他'),
    allowNull: false,
    defaultValue: '身份证',
    comment: '证件类型'
  },
  idNumber: {
    type: DataTypes.STRING(30),
    allowNull: false,
    comment: '证件号码'
  },
  applicableSupplies: {
    type: DataTypes.TEXT,
    allowNull: true,
    comment: '适用生资（JSON格式存储）'
  },
  supervisorySuppliesQuantity: {
    type: DataTypes.INTEGER,
    allowNull: false,
    defaultValue: 0,
    comment: '监管生资数量'
  },
  taskStatus: {
    type: DataTypes.ENUM('待处理', '处理中', '已完成', '已取消'),
    allowNull: false,
    defaultValue: '待处理',
    comment: '任务状态'
  },
  priority: {
    type: DataTypes.ENUM('低', '中', '高', '紧急'),
    allowNull: false,
    defaultValue: '中',
    comment: '任务优先级'
  },
  assignedTo: {
    type: DataTypes.INTEGER,
    allowNull: true,
    comment: '分配给（用户ID）'
  },
  dueDate: {
    type: DataTypes.DATE,
    allowNull: true,
    comment: '截止日期'
  },
  completedAt: {
    type: DataTypes.DATE,
    allowNull: true,
    comment: '完成时间'
  },
  notes: {
    type: DataTypes.TEXT,
    allowNull: true,
    comment: '备注信息'
  },
  createdBy: {
    type: DataTypes.INTEGER,
    allowNull: true,
    comment: '创建人ID'
  },
  updatedBy: {
    type: DataTypes.INTEGER,
    allowNull: true,
    comment: '更新人ID'
  }
}, {
  tableName: 'supervisory_tasks',
  timestamps: true,
  underscored: true,
  indexes: [
    {
      name: 'idx_application_number',
      fields: ['applicationNumber']
    },
    {
      name: 'idx_policy_number', 
      fields: ['policyNumber']
    },
    {
      name: 'idx_customer_name',
      fields: ['customerName']
    },
    {
      name: 'idx_task_status',
      fields: ['taskStatus']
    },
    {
      name: 'idx_created_at',
      fields: ['createdAt']
    }
  ]
});

// 定义关联关系
SupervisoryTask.associate = (models) => {
  // 监管任务属于用户（分配给）
  SupervisoryTask.belongsTo(models.User, {
    foreignKey: 'assignedTo',
    as: 'assignedUser'
  });
  
  // 监管任务属于创建者
  SupervisoryTask.belongsTo(models.User, {
    foreignKey: 'createdBy',
    as: 'creator'
  });
  
  // 监管任务属于更新者
  SupervisoryTask.belongsTo(models.User, {
    foreignKey: 'updatedBy',
    as: 'updater'
  });
};

module.exports = SupervisoryTask;