import BaseModel from './BaseModel';
import { index, getModelForClass, prop, DocumentType } from '@typegoose/typegoose';

/**
  * 寻宝中图纸掉落的伪随机记录
  */
@index({ roleId: 1 })

export default class EquipPrintDrop extends BaseModel {
  @prop({ required: true, unique: true })
  roleId: string;					// 角色 id
  @prop({ required: true})
  roleName: string; // 角色名

  @prop({ required: true, default: 0 })
  capTarget: number;						// 本轮第几次会掉落，本轮结束后重置
  @prop({ required: true, default: 0 })
  capCur: number;						// 本轮进行的次数

  @prop({ required: true, default: 0 })
  teammateTarget: number;						// 本轮第几次会掉落，本轮结束后重置
  @prop({ required: true, default: 0 })
  teammateCur: number;						// 本轮进行的次数

  @prop({ required: true, default: 0 })
  capDropAll: number;						// 队长掉落总次数，累积
  @prop({ required: true, default: 0 })
  teammateDropAll: number;						// 队员掉落总次数，累积

  @prop({ required: true, default: 0 })
  capAll: number;						// 队长随机总次数，累积
  @prop({ required: true, default: 0 })
  teammateAll: number;						// 队员随机总次数，累积

  public static async getByRoleId(roleId: string, lean = true) {
    return await EquipPrintDropModel.findOne({roleId}).lean(lean);
  }

  public static async createDoc(docData: EquipPrintDropType, lean = true) {
    return await EquipPrintDropModel.findOneAndUpdate({roleId: docData.roleId}, {...docData}, {upsert: true, new: true}).lean(lean);
  }

  public static async updateDoc(roleId: string, docData: Partial<EquipPrintDropType>, lean = true) {
    return await EquipPrintDropModel.findOneAndUpdate({roleId}, {...docData}, {new: true}).lean(lean);
  }
}

export const EquipPrintDropModel = getModelForClass(EquipPrintDrop);

export interface EquipPrintDropType extends Pick<DocumentType<EquipPrintDrop>, keyof EquipPrintDrop>{}