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

class Vestige {
    @prop({ required: true })
    vestigeId: number;   // 遗迹点

    @prop({ required: true })
    position: string;   // 遗迹点位置
}

@index({ groupId: 1, day: 1 })
export default class GVGVestige extends BaseModel {

    @prop({ required: true })
    groupKey: string;          // 战区

    @prop({ required: true, type: Vestige, _id: false })
    vestiges: Vestige[];   // 今天的遗迹点

    @prop({ required: true })
    day: number;           // 每天5点

    public static async getVestige(groupKey: string) {
        let day = getZeroPoint();
        let result: GVGVestigeType = await GVGVestigeModel.findOne({ groupKey, day }).lean();
        return result;
    }

    public static async initTodayVestige(groupKey: string, vestiges: Vestige[]) {
        let day = getZeroPoint();
        let result: GVGVestigeType = await GVGVestigeModel.findOneAndUpdate({ groupKey, day }, { $setOnInsert: { vestiges } }, { new: true, upsert: true }).lean();
        return result;
    }
}

export const GVGVestigeModel = getModelForClass(GVGVestige);

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