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

@index({ configId: 1, pontId: 1, groupKey: 1 })
// GVGCityAreaPoint 类，继承自 BaseModel
export default class GVGCityAreaPoint extends BaseModel {
    @prop({ required: true, default: 1 })
    configId: number;       // config唯一id 

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

    @prop({ required: true })
    cityId: number;         // 城池id

    @prop({ required: true })
    areaId: number;         // 区域

    @prop({ required: true })
    pointId: number;         // 积分点

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

    @prop({ required: true })
    leagueName: string;         // 联军

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

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

    @prop({ required: true })
    teamCode: string;         // 玩家的编队

    public static async settlePoint(cityId: number, areaId: number, pointId: number, team: GVGTeamType, originTeam = '') {
        let { configId, groupKey, leagueCode, leagueName, roleId, roleName, teamCode } = team;
        await GVGCityAreaPointModel.updateMany({ configId, groupKey, teamCode }, { $set: { teamCode: '', roleId: '', roleName: '', leagueCode: '', leagueName: '' } });
        await GVGCityAreaPointModel.findOneAndUpdate({ configId, groupKey, pointId }, { $setOnInsert: { teamCode: '' }}, { upsert: true });
        let result: GVGCityAreaPointType = await GVGCityAreaPointModel.findOneAndUpdate(
            { configId, groupKey, pointId, teamCode: originTeam },
            { $set: { cityId, areaId, leagueCode, leagueName, roleId, roleName, teamCode } },
            { new: true }).lean();
        return result;
    }

    public static async leavePoint(configId: number, groupKey: string, pointId: number) {
        let result: GVGCityAreaPointType = await GVGCityAreaPointModel.findOneAndUpdate({ configId, groupKey, pointId }, { $set: { teamCode: '', roleId: '', roleName: '', leagueCode: '', leagueName: '' }}, { new: true }).lean();
        return result;
    }

    public static async playerLeave(configId: number, groupKey: string, roleId: string) {
        await GVGCityAreaPointModel.updateMany({ configId, groupKey, roleId }, { $set: { teamCode: '', roleId: '', roleName: '', leagueCode: '', leagueName: '' }});
    }

    public static async findByConfig(configId: number, groupKey: string) {
        let result: GVGCityAreaPointType[] = await GVGCityAreaPointModel.find({ configId, groupKey }).lean();
        return result
    }

    public static async updateLeagueName(leagueCode: string, leagueName: string) {
        await GVGCityAreaPointModel.updateMany({ leagueCode }, { $set: { leagueName }});
    }
}


export const GVGCityAreaPointModel = getModelForClass(GVGCityAreaPoint);

export interface GVGCityAreaPointType extends Pick<DocumentType<GVGCityAreaPoint>, keyof GVGCityAreaPoint> {};

export type GVGCityAreaPointUpdate = Partial<GVGCityAreaPointType>; // 将所有字段变成可选项

