/**
 * 秘境首通
 */
import BaseModel from './BaseModel';
import { getModelForClass, prop, DocumentType, index } from '@typegoose/typegoose';
import { RankParam, LineupParam } from '../domain/rank';

@index({ serverId: 1, movePoint: 1 })
export default class DungeonFirst extends BaseModel {
    
    @prop({ required: true })
    serverId: number;                    // 服务器id

    @prop({ required: true, default: 0})
    movePoint: number;                    // 秘境的类型
    
    @prop({ required: true, default: 0})
    warId: number;                        // 关卡id

    @prop({ required: true })
    time: number;                         // 首通时间

    @prop({ required: true })
    roleId: string;                         // 玩家id
    
    @prop({ required: true, type: () => RankParam, _id: false })
    userInfo: RankParam;                         // 玩家信息

    @prop({ required: true, type: () => LineupParam, _id: false })
    lineup: LineupParam[];                         // 玩家信息

    public static async createDungeonFirst(serverId: number, movePoint: number, warId: number, update: DungeonFirstUpdateParam) {
        const rec: DungeonFirstType = await DungeonFirstModel.findOneAndUpdate({ serverId, movePoint, warId}, { $setOnInsert: update }, { new: true, upsert: true }).lean();
        return rec;
    }

    public static async findByMovePoint(serverId: number, movePoint: number) {
        const rec: DungeonFirstType[] = await DungeonFirstModel.find({ serverId, movePoint}).lean();
        return rec;
    }
}

export const DungeonFirstModel = getModelForClass(DungeonFirst);

export interface DungeonFirstType extends Pick<DocumentType<DungeonFirst>, keyof DungeonFirst>{}
export type DungeonFirstUpdateParam = Partial<DungeonFirstType>; // 将所有字段变成可选项
