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

class Report {
    @prop({ required: true })
    roleName: string;
    @prop({ required: true })
    trainId: number;//挑战的试炼关 
    @prop({ required: true })
    hid: number;//据点
    @prop({ required: true })
    score: number;
    @prop({ required: true })
    time: number;
    @prop({ required: true })
    difficulty: number;
    @prop({ required: true })
    type: number;//1：失败， 2：成功，3：表示系统战报即：被成功压制
}
@index({guildCode: 1, trainId: 1})

export default class GuildTrainReport extends BaseModel {
    @prop({ required: true })
    guildCode: string;            
    @prop({ required: true })
    trainId: number;    
    @prop({ required: true, default: [], type: Report, _id: false  })
    reports: Report[];

    public static async pushGuildTrainReports(guildCode: string, trainId: number,  reports: Array<Report>, lean = true) {
        const guildTrainReport: GuildTrainReportType = await GuildTrainReportModel.findOneAndUpdate({ guildCode, trainId }, 
        { $push: {reports:{ $each:reports } } },{new: true}).lean(lean);
        return guildTrainReport;
    }

    public static async resetGuildTrainReport(guildCode: string, trainId: number, lean = true) {
        const guildTrainReport: GuildTrainReportType = await GuildTrainReportModel.findOneAndUpdate({ guildCode, trainId }, 
            { $set: {reports:[], guildCode, trainId } }, {new: true, upsert: true}).lean(lean);
            return guildTrainReport;
    }

    public static async findGuildTrainByTrainIds(guildCode: string, trainIds:Array<number>, lean = true) {
        const guildTrains: GuildTrainReportType[] = await GuildTrainReportModel.find({ trainId:{ $in:trainIds}, guildCode }).lean(lean);
        return guildTrains;
    }

}

export const GuildTrainReportModel = getModelForClass(GuildTrainReport);

export interface GuildTrainReportType extends Pick<DocumentType<GuildTrainReport>, keyof GuildTrainReport> { };