import BaseModel from './BaseModel';
import { index, getModelForClass, prop, DocumentType } from '@typegoose/typegoose';
class Reward {
    @prop({ required: true })
    id: number;
    @prop({ required: true })
    count: number;
}

class HeroReward {
    @prop({ required: true })
    hid: number;
    @prop({ required: true })
    id: number;
    @prop({ required: true })
    count: number;
}
export class HeroScores {
    @prop({ required: true })
    hid: number;
    @prop({ required: true })
    score: number;
    @prop({ required: true })
    goodId: number;
    @prop({ required: true })
    count: number;
}

export interface pvpUpdate {
    rankGoods?:Array<Reward>;
    oldSeasonData?: SeasonData;
    show?: boolean;
    heroGoods?:Array<HeroReward>;
}

class SeasonData {
    @prop({ required: true })
    score: number;

    @prop({ required: true })
    pLv: number;

    @prop({ required: true, type:HeroScores, default:[] })
    heroScores: HeroScores[];

    @prop({ required: true })
    seasonNum: number;

    @prop({ required: true })
    challengeCnt: number;

    @prop({ required: true })
    challengeRefTime: number;

    @prop({ required: true })
    seasonEndTime: number;

    @prop({ required: true })
    rankLv: number;

    @prop({ required: true })
    refOppCnt:number;
}

@index({ roleId: 1 })
export default class PvpSeasonResult extends BaseModel {
    @prop({ required: true })
    roleId: string;

    @prop({ required: true, type: SeasonData, _id: false})
    oldSeasonData: SeasonData;
    
    @prop({ required: true,  default: true })
    show: boolean;

    @prop({ required: true, type: HeroReward, default: [], _id: false})
    heroGoods: Array<HeroReward>;

    @prop({ required: true, type: Reward, default: [], _id: false})
    rankGoods: Array<Reward>;

    public static async updatePvpSeasonResult(roleId: string, update: pvpUpdate , lean = true) {
        let result: PvpSeasonResultType = await PvpSeasonResultModel.findOneAndUpdate({roleId}, {$set: update}, {upsert: true, new: true}).lean(lean);
        return result;
    }
    public static async getPvpSeasonResult(roleId: string, lean = true) {
        let result: PvpSeasonResultType = await PvpSeasonResultModel.findOne({roleId}).lean(lean);
        return result;
    }
}

export const PvpSeasonResultModel = getModelForClass(PvpSeasonResult);

export interface PvpSeasonResultType extends Pick<DocumentType<PvpSeasonResult>, keyof PvpSeasonResult> { };