import {ApiCall} from "tsrpc";
import {PlayerFun} from '../../../public/player';
import {ReqRec, ResRec} from "../../../shared/protocols/event/yuedujijin/PtlRec";
import {PayFun} from "../../../public/pay";

export default async function (call: ApiCall<ReqRec, ResRec>) {
    let type = call.req.type as `${number}jijin`;
    let conf = G.gc.yuedujijin.find(v => v.payId == type);
    if (!conf) return call.errorCode(-1);
    if (!conf.prize[call.req.index]) return call.errorCode(-2);

    let db = await G.mongodb.cEvent(type).findOne({uid: call.uid, type: type});
    let day = db?.days - (db?.round || 0) * 30 || 0;
    let recLength = db?.rec?.length % 30
    let rec = recLength ? (db?.rec?.slice(-recLength % 30) || []) : []
    if (call.req.index > day) return call.errorCode(-3);
    if (rec.includes(call.req.index)) return call.errorCode(-4);


    G.mongodb.cEvent(type).updateOne({uid: call.uid, type: type}, {$push: {rec: call.req.index}}, {upsert: true});


    await PlayerFun.sendPrize(call, [].concat(conf.prize[call.req.index]));

    call.succ({
        prize: [].concat(conf.prize[call.req.index])
    });
}