import BaseModel from './BaseModel';
import { index, getModelForClass, prop, DocumentType } from '@typegoose/typegoose';
import { ItemReward } from '../domain/dbGeneral';
import { getZeroPointD } from '../pubUtils/timeUtil';
import { genCode } from '../pubUtils/util';
import { Member, WoodenHorse, Event } from '../domain/battleField/guildActivity'


@index({ code: 1 })
export default class GuildActivityRecord extends BaseModel {

    @prop({ required: true })
    code: string;            // 本次记录唯一值
    
    @prop({ required: true })
    guildCode: string;       // 军团code

    @prop({ required: true })
    serverId: number;       // 分服

    @prop({ required: true, default: 1 })
    status: number;       // 分服

    @prop({ required: true })
    aid: number;             // 军团活动id

    @prop({ required: true })
    auctionType: number;             // 转为拍卖行的类型

    @prop({ required: true, default: false })
    isSuccess: boolean;      // 是否胜利

    @prop({ required: true, default: false })
    isCompleted: boolean;      // 是否胜利

    @prop({ required: true })
    rank: number;            // 最终排名

    @prop({ required: true, type: Member, default: [], _id: false })
    members: Member[];        // 参与活动的团员的roleId列表

    @prop({ required: true, default: 0 })
    memberCnt: number;        // 参加的团员的数量

    @prop({ required: true, type: ()=> ItemReward, default: [], _id: false })
    rewards: ItemReward[];        // 转到拍卖行的道具

    @prop({ required: true })
    score: number;            // 蛮夷入侵总军功

    @prop({ required: true })
    remainGateHp: number;           // 城门剩余血量

    @prop({ required: true })
    cityId: number;           // 诸侯混战占领了的城池

    @prop({ required: true })
    damage: number;           // 诸侯混战

    @prop({ required: true, _id: false })
    woodenHorse: WoodenHorse;   // 粮草先行木马

    @prop({ required: true, type: () => Event,  _id: false })
    events: Event[];   // 粮草先行木马

    // 每天一条记录
    public static async getRecord(guildCode: string, serverId: number, aid: number, events?: Event[]) {
        let today = getZeroPointD();
        let docs = new GuildActivityRecordModel();
        let code = genCode(10);
        let update = Object.assign(docs, { code, guildCode, serverId, aid, createdAt: new Date() });
        if(events) update = Object.assign(update, { events });
        let rec: GuildActivityRecordType = await GuildActivityRecordModel.findOneAndUpdate(
            { aid, guildCode, createdAt: { $gte: today }, status: 1}, 
            { $setOnInsert: update }, 
            {new: true, upsert: true}).lean();
        return rec;
    }
    
    public static async updateInfo(guildCode: string, update: GuildActivityRecordUpdateParam) {
        let today = getZeroPointD();

        let rec: GuildActivityRecordType = await GuildActivityRecordModel.findOneAndUpdate(
            { guildCode, createdAt: { $gte: today } }, 
            { $set: update }, { new: true }).lean();
        return rec;
    }

    public static async updateTodayInfo(update: GuildActivityRecordUpdateParam) {
        let today = getZeroPointD();

        let rec = await GuildActivityRecordModel.updateMany(
            { createdAt: { $gte: today } }, 
            { $set: update }, { new: true }).lean();
        return rec;
    }

    public static async pushEvent(guildCode: string, event: Event) {
        let today = getZeroPointD();

        let rec: GuildActivityRecordType = await GuildActivityRecordModel.findOneAndUpdate(
            { guildCode, createdAt: { $gte: today } }, 
            { $push: { events: event } }, { new: true }).lean();
        return rec;
    }

    public static async findBySourceCode(guildCode: string, sourceType: number, sourceCode: string) {
        const rec = await GuildActivityRecordModel.findOne({ guildCode, auctionType: sourceType, code: sourceCode }).select('members').lean();
        return rec;
    }
}

export const GuildActivityRecordModel = getModelForClass(GuildActivityRecord);

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