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

export class Record {
    @prop({ required: true })
    count: number;			        // 次数
    @prop({ required: true })
    reward: string;			        // 奖励内容
    @prop({ required: true })
    time: Date;				        // 抽卡时间
}

/**
  * 活动系统 - 每日关卡活动
*/
@index({ roleId: 1 })

export default class Activity_New_Hero_Gacha extends BaseModel {
    @prop({ required: true })
    serverId: number;				// 服id
    @prop({ required: true })
    activityId: number;				// 活动Id
    @prop({ required: true })
    roleId: string;			        // 用户Id
    @prop({ required: true })
    selected: boolean;				    // 是否选中默认false
    @prop({ required: true })
    hid: number;				        // 武将id
    @prop({ required: true })
    count: number;				        // 抽卡次数，抽中大奖后重新统计
    @prop({ required: true })
    records: Record[];				    // 抽卡历史记录

    //重置统计次数
    public static async resetCount(serverId: number, activityId: number, roleId: string, hid: number, count: number) {
        let result: ActivityNewHeroGachaModelType = await ActivityNewHeroGachaModel.findOneAndUpdate({ serverId, roleId, activityId, hid },
            { $set: { count } }, { upsert: true, new: true }).lean(true);
        return result;
    }

    //抽卡记录
    public static async addRecord(serverId: number, activityId: number, roleId: string, hid: number, count: number, reward: string) {
        let result: ActivityNewHeroGachaModelType = await ActivityNewHeroGachaModel.findOneAndUpdate({ serverId, roleId, activityId, hid },
            { $inc: { count }, $push: { records: { count, reward, time: new Date() } } }, { upsert: true, new: true }).lean(true);
        return result;
    }

    //设置选中武将
    public static async selectedHero(serverId: number, activityId: number, roleId: string, hid: number, selected: boolean) {
        let result: ActivityNewHeroGachaModelType[] = await ActivityNewHeroGachaModel.findOneAndUpdate(
            { serverId, roleId, activityId, hid }, { $set: { selected } }, { upsert: true, new: true }).lean(true);
        return result;
    }

    //根据活动id查询活动数据
    public static async findDataByHid(serverId: number, activityId: number, roleId: string, hid: number) {
        let result: ActivityNewHeroGachaModelType = await ActivityNewHeroGachaModel.findOne({ serverId, roleId, activityId, hid }).lean(true);
        return result;
    }

    //根据活动id查询活动数据
    public static async findData(serverId: number, activityId: number, roleId: string) {
        let result: ActivityNewHeroGachaModelType[] = await ActivityNewHeroGachaModel.find({ serverId, roleId, activityId }).lean(true);
        return result;
    }

    //删除活动领取记录
    public static async deleteActivity(serverId: number, activityId: number, roleId: string) {
        await ActivityNewHeroGachaModel.deleteMany({ serverId, roleId, activityId });
    }
}

export const ActivityNewHeroGachaModel = getModelForClass(Activity_New_Hero_Gacha);

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