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

@index({ leagueCode: 1, guildCode: 1, configId: 1 })
@index({ cityId: 1, configId: 1 })
@index({ createTime: -1 })
export default class GVGRec extends BaseModel {

    @prop({ required: false, default: '' })
    leagueCode: string;        // 联军id

    @prop({ required: false, default: '' })
    groupKey: string;            // 战区

    @prop({ required: false, default: '' })
    roleId: string;            // 玩家id

    @prop({ required: false, default: '' })
    cityId: number;            // 所处城池

    @prop({ required: true, default: 0 })
    configId: number;          // 赛期

    @prop({ required: true, default: 0 })
    type: number;              // 1-备战期联军动态 2-激战期我的战报 3-激战期城池战报 4-激战期军团战报

    @prop({ required: true, default: 0 })
    recId: number;              // 配在动态策划表中的id

    @prop({ required: true, default: [], type: String })
    params: string[];           // 显示的参数问题，替代%d的

    @prop({ required: true, default: 0 })
    createTime: number;          // 时间

    public static async addRec(param: GVGRecUpdate) {
        await GVGRecModel.insertMany([param]);
    }

    public static async addRecs(params: GVGRecUpdate[]) {
        await GVGRecModel.insertMany(params);
    }

    public static async findByLeague(leagueCode: string, type: GVG_REC_TYPE) {
        const result: GVGRecType[] = await GVGRecModel.find({ leagueCode, type }, { _id: 0, recId: 1, params: 1, createTime: 1 }).sort({ createTime: -1 }).limit(50).lean();
        return result;
    }

    // 查询城池战报
    public static async findBattleRecByCity(configId: number, groupKey: string, cityId: number) {
        const result: GVGRecType[] = await GVGRecModel.find({ configId, groupKey, type: GVG_REC_TYPE.BATTLE_BY_CITY, cityId }, { _id: 0, recId: 1, params: 1, createTime: 1 }).sort({ createTime: -1 }).limit(50).lean();
        return result;
    }
    
    public static async findBattleRecByGroup(configId: number, groupKey: string) {
        const result: GVGRecType[] = await GVGRecModel.find({ configId, groupKey, type: GVG_REC_TYPE.BATTLE_BY_CITY }, { _id: 0, recId: 1, params: 1, createTime: 1 }).sort({ createTime: -1 }).limit(50).lean();
        return result;
    }

    public static async findBattleRecByLeague(configId: number, leagueCode: string) {
        const result: GVGRecType[] = await GVGRecModel.find({ configId, leagueCode, type: GVG_REC_TYPE.BATTLE_BY_LEAGUE }, { _id: 0, recId: 1, params: 1, createTime: 1 }).sort({ createTime: -1 }).limit(50).lean();
        return result;
    }

    public static async findBattleRecByRole(configId: number, roleId: string, leagueCode: string) {
        const result: GVGRecType[] = await GVGRecModel.find({ configId, roleId, leagueCode, type: GVG_REC_TYPE.BATTLE_BY_ROLE }, { _id: 0, recId: 1, params: 1, createTime: 1 }).sort({ createTime: -1 }).limit(50).lean();
        return result;
    }
}

export const GVGRecModel = getModelForClass(GVGRec);

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