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

@index({ serverId: 1, guildCode: 1, status: 1, declareIndex: 1  })
export default class GuildActivityCityDeclare extends BaseModel {

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

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

    @prop({ required: true })
    guildCode: string;       // 宣战的公会的code

    @prop({ required: true })
    declareTime: number;       // 宣战时间（当前）

    @prop({ required: true })
    declareMember: string;       // 触发宣战的人

    @prop({ required: true })
    isAutoDeclare: boolean;       // 是否是自动宣战

    @prop({ required: true })
    status: number;       // 状态 1-当前 0-失效，当活动结束时定时任务设为0

    // 宣战
    public static async declareIfNot(serverId: number, guildCode: string, cityId: number, roleId: string, declareTime: number, isAutoDeclare: boolean, declareIndex: number) {
        let rec: GuildActivityCityDeclareType = await GuildActivityCityDeclareModel.findOneAndUpdate({ serverId, guildCode, status: 1, declareIndex }, { $setOnInsert: { declareMember: roleId, declareTime, isAutoDeclare, cityId } }, { new: true, upsert: true }).lean();
        return rec;
    }

    // 检查是否宣战过
    public static async checkDeclartion(serverId: number, guildCode: string, declareIndex: number) {
        let rec = await GuildActivityCityDeclareModel.exists({ serverId, status: 1, guildCode, declareIndex});
        return rec;
    }
}

export const GuildActivityCityDeclareModel = getModelForClass(GuildActivityCityDeclare);

export interface GuildActivityCityDeclareType extends Pick<DocumentType<GuildActivityCityDeclare>, keyof GuildActivityCityDeclare> { };
export type GuildActivityCityDeclareUpdateParam = Partial<GuildActivityCityDeclareType>; // 将所有字段变成可选项