import BaseModel from './BaseModel';
import { index, getModelForClass, prop, DocumentType, modelOptions } from '@typegoose/typegoose';
import { GachaResult } from '../domain/activityField/gachaField';
import { genCode } from '../pubUtils/util';

/**
  * 玩家抽卡表
**/
@modelOptions({ schemaOptions: { id: false } })
@index({ code: 1 })

export default class UserGachaRec extends BaseModel {

    @prop({ required: true })
    code: string;					// 玩家id

    @prop({ required: true })
    roleId: string;					// 玩家id

    @prop({ required: true })
    gachaId: number;			    // 抽卡id 1-元宝 2-友情 3-指定 4-限时

    @prop({ required: true, default: 0 })
    activityId: number;			        // 限时抽卡对应活动id

    @prop({ required: true, default: 0 })
    count: number;			      // 抽卡次数

    @prop({ required: true, type: GachaResult, default: [], _id: false })
    result: GachaResult[];             // 结果

    public static async createRec(roleId: string, gachaId: number, activityId: number, count: number, result: GachaResult[]) {
        let code = genCode(8);
        const rec = await UserGachaRecModel.findOneAndUpdate({ code }, { $set: { roleId, gachaId, activityId, count, result } }, { new: true, upsert: true });
        return rec;
    }
}

export const UserGachaRecModel = getModelForClass(UserGachaRec);

export interface UserGachaRecType extends Pick<DocumentType<UserGachaRec>, keyof UserGachaRec> { }
export type UserGachaRecParam = Partial<UserGachaRecType>;