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

/**
  * 情谊点记录
  */
@index({ roleId: 1, createdAt: -1 })

export default class FriendPoint extends BaseModel {
    @prop({ required: true, default: '' })
    roleId: string;	// 角色 id
    @prop({ required: true, default: '' })
    roleName: string; // 角色名称
    @prop({ required: true, default: 0 })
    cnt: number; // 当天获取的点数
    @prop({ required: true, default: 0 })
    sendCnt: number; // 当天赠送的点数
    @prop({ required: true, default: 0 })
    comBattleCnt: number; // 今天在寻宝上获得的友情点
    @prop({ required: true, default: 0 })
    type: number; // 情谊点统计的类型，不同功能都可能有每日获取上限

    /**
     * @description 更新每天的情谊点获取情况，cnt 有可能超过当日最大值，可根据返回值判断
     * @static
     * @param {string} roleId 用户 Id
     * @param {string} roleName 用户名
     * @param {number} cntInc 增长数量
     * @param {number} maxPerDay 每日最大数量
     * @param {boolean} [lean=true]
     * @memberof FriendPoint
     */
    public static async updatePointToday(roleId: string, roleName: string, cntInc: number, maxPerDay: number, type: number, lean = true) {
        const curTime = getZeroPointD();
        // 当 oldCnt + cntInc > maxPerDay 时需计算实际可以获得的点数：cntInc - (newCnt - maxPerDay)
        const rec: FriendPointType = await FriendPointModel.findOneAndUpdate({roleId, roleName, createdAt: {$gte: curTime}, cnt: {$lt: maxPerDay}, type}, {$inc: {cnt: cntInc, sendCnt: 0}}, {upsert: true, new: true}).lean(lean);
        return rec;
    }

    public static async updateSendCntToday(roleId: string, roleName: string, cntInc: number, maxPerDay: number, type: number, lean = true) {
        const curTime = getZeroPointD();
        // 当 oldCnt + cntInc > maxPerDay 时需计算实际可以获得的点数：cntInc - (newCnt - maxPerDay)
        const rec: FriendPointType = await FriendPointModel.findOneAndUpdate({roleId, roleName, createdAt: {$gte: curTime}, sendCnt: {$lt: maxPerDay}, type}, {$inc: {sendCnt: cntInc, cnt: 0}}, {upsert: true, new: true}).lean(lean);
        return rec;
    }

    public static async updateComBattleCntToday(roleId: string, roleName: string, cntInc: number, maxPerDay: number, type: number, lean = true) {
        const curTime = getZeroPointD();
        // 当 oldCnt + cntInc > maxPerDay 时需计算实际可以获得的点数：cntInc - (newCnt - maxPerDay)
        const rec: FriendPointType = await FriendPointModel.findOneAndUpdate({roleId, roleName, createdAt: {$gte: curTime}, comBattleCnt: {$lt: maxPerDay}, type}, {$inc: {comBattleCnt: cntInc}}, {upsert: true, new: true}).lean(lean);
        return rec;
    }

    /**
     * @description 某个类型当天情谊点获取情况
     * @static
     * @param {string} roleId 用户 Id
     * @param {number} type 掉落情谊点的功能类型 FRIEND_DROP_TYPE
     * @param {boolean} [lean=true]
     * @returns
     * @memberof FriendPoint
     */
    public static async getFrdPointRecToday(roleId: string, type: number, lean = true) {
        const curTime = getZeroPointD();
        const rec: FriendPointType = await FriendPointModel.findOne({roleId, createdAt: {$gte: curTime}, type}).lean(lean);
        return rec;
    }
}

export const FriendPointModel = getModelForClass(FriendPoint);

export interface FriendPointType extends Pick<DocumentType<FriendPoint>, keyof FriendPoint>{};