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

/**
  * 玩家每日任务，每天刷掉的旧任务汇总
**/
@modelOptions({ schemaOptions: { id: false } })
@index({ roleId: 1 })

export default class UserTaskHistory extends BaseModel {

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

    @prop({ required: true, type: UserTaskRec, default: [] })
    list: UserTaskRec[];  	    	// 是否已领取

    public static async pushUserTask(roleId: string, userTaskRec: UserTaskRecType[]) {
        let rec: UserTaskHistoryType = await UserTaskHistoryModel.findOneAndUpdate({ roleId }, { $push: { list: { $each: userTaskRec } } }, { new: true, upsert: true }).lean();
        return rec;
    }

}

export const UserTaskHistoryModel = getModelForClass(UserTaskHistory);

export interface UserTaskHistoryType extends Pick<DocumentType<UserTaskHistory>, keyof UserTaskHistory> { }
export type UserTaskHistoryParam = Partial<UserTaskHistoryType>;
