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

@index({ leagueCode: 1, roleId: 1, configId: 1, time: 1 })
export default class GVGUserDailyData extends BaseModel {

    @prop({ required: true })
    roleId: string;          // 玩家

    @prop({ required: true })
    configId: number;        // 赛季信息

    @prop({ required: true })
    leagueCode: string;      // 所属联军

    @prop({ required: true, default: 0 })
    job: number;             // 职能 1-贤臣 2-猛将

    // 贡献
    @prop({ required: true, default: 0 })
    food: number;        // 粮食

    @prop({ required: true, default: 0 })
    mineral: number;     // 矿物

    @prop({ required: true, default: 0 })
    wood: number;        // 木材

    @prop({ required: true, default: 0 })
    score: number;       // 据点积分

    @prop({ required: true, default: 0 })
    time: number = 0;       // 每天0点

    @prop({ required: true, default: 0 })
    hasGetTechActive: boolean;       // 千机阁激活活跃是否获得

    @prop({ required: true, default: 0 })
    hasLogin: boolean;       // 是否登录

    public static async findByRole(configId: number, leagueCode: string, roleId: string) {
        const result: GVGUserDailyDataType = await GVGUserDailyDataModel.findOne({ configId, leagueCode, roleId, time: getZeroPoint() }).select('food mineral wood score').lean();
        return result;
    }

    public static async checkGetTechActive(configId: number, leagueCode: string, roleId: string) {
        return await GVGUserDailyDataModel.exists({ configId, leagueCode, roleId, time: getZeroPoint(), hasGetTechActive: { $exists: true } });
    }

    public static async setHasGetTechActive(configId: number, leagueCode: string, roleId: string) {
        const result: GVGUserDailyDataType = await GVGUserDailyDataModel.findOneAndUpdate({ configId, leagueCode, roleId, time: getZeroPoint() }, { hasGetTechActive: true }, { upsert: true, new: true }).lean();
        return result;
    }

    public static async checkHasLogin(configId: number, leagueCode: string, roleId: string) {
        return await GVGUserDailyDataModel.exists({ configId, leagueCode, roleId, time: getZeroPoint(), hasLogin: { $exists: true } });
    }

    public static async setHasLogin(configId: number, leagueCode: string, roleId: string) {
        const result: GVGUserDailyDataType = await GVGUserDailyDataModel.findOneAndUpdate({ configId, leagueCode, roleId, time: getZeroPoint() }, { hasLogin: true }, { upsert: true, new: true }).lean();
        return result;
    }

    public static async addResource(configId: number, leagueCode: string, roleId: string, food: number, mineral: number, wood: number) {
        const result: GVGUserDailyDataType = await GVGUserDailyDataModel.findOneAndUpdate({ configId, leagueCode, roleId, time: getZeroPoint()}, { $inc: { food, mineral, wood }}, { new: true, upsert: true }).lean();
        return result;
    }
}

export const GVGUserDailyDataModel = getModelForClass(GVGUserDailyData);

export interface GVGUserDailyDataType extends Pick<DocumentType<GVGUserDailyData>, keyof GVGUserDailyData> {
    id: number;
};
export type GVGUserDailyDataUpdate = Partial<GVGUserDailyDataType>; // 将所有字段变成可选项
