/**
 * 邀请记录表模型
 * 记录用户邀请注册的信息
 */

const Sequelize = require('sequelize');

module.exports = (db) => {
  const invite_record = db.define("invite_record", {
    // 邀请人ID（关联 pla_account.id）
    inviter_id: {
      comment: '邀请人ID',
      type: Sequelize.INTEGER,
      allowNull: false,
      references: {
        model: 'pla_account',
        key: 'id'
      }
    },
    // 邀请人SN码
    inviter_sn_code: {
      comment: '邀请人SN码',
      type: Sequelize.STRING(50),
      allowNull: false,
      defaultValue: ''
    },
    // 被邀请人ID（关联 pla_account.id）
    invitee_id: {
      comment: '被邀请人ID',
      type: Sequelize.INTEGER,
      allowNull: false,
      references: {
        model: 'pla_account',
        key: 'id'
      }
    },
    // 被邀请人SN码
    invitee_sn_code: {
      comment: '被邀请人SN码',
      type: Sequelize.STRING(50),
      allowNull: false,
      defaultValue: ''
    },
    // 被邀请人手机号
    invitee_phone: {
      comment: '被邀请人手机号',
      type: Sequelize.STRING(20),
      allowNull: false,
      defaultValue: ''
    },
    // 邀请码
    invite_code: {
      comment: '邀请码',
      type: Sequelize.STRING(100),
      allowNull: false,
      defaultValue: ''
    },
    // 注册时间
    register_time: {
      comment: '注册时间',
      type: Sequelize.DATE,
      allowNull: false,
      defaultValue: Sequelize.NOW
    },
    // 奖励状态（0=未发放，1=已发放）
    reward_status: {
      comment: '奖励状态（0=未发放，1=已发放）',
      type: Sequelize.TINYINT(1),
      allowNull: false,
      defaultValue: 0
    },
    // 奖励类型（trial_days=试用期天数）
    reward_type: {
      comment: '奖励类型',
      type: Sequelize.STRING(50),
      allowNull: false,
      defaultValue: 'trial_days'
    },
    // 奖励值（试用期天数）
    reward_value: {
      comment: '奖励值（试用期天数）',
      type: Sequelize.INTEGER,
      allowNull: false,
      defaultValue: 3
    },
   
    // 备注
    remark: {
      comment: '备注',
      type: Sequelize.TEXT,
      allowNull: true
    }
  }, {
    tableName: 'invite_record',
    indexes: [
      {
        name: 'idx_inviter_id',
        fields: ['inviter_id']
      },
      {
        name: 'idx_invitee_id',
        fields: ['invitee_id']
      },
      {
        name: 'idx_invite_code',
        fields: ['invite_code']
      },
      {
        name: 'idx_register_time',
        fields: ['register_time']
      },
      {
        name: 'idx_reward_status',
        fields: ['reward_status']
      }
    ]
  });

  // invite_record.sync({ alter: true });

  return invite_record;
};

