import { ApiCall } from "tsrpc";
import { PayFun } from '../../../public/pay';
import { jijinType } from "../../../shared/protocols/event/jijin/PtlOpen";
import {ReqCheckOpen,ResCheckOpen} from "../../../shared/protocols/event/jijin/PtlCheckOpen";


const type:jijinType[] = [
    'dengjijijin',
    'guanqiajijin',
    'dayjijin',
    'tianshujijin',
]

export default async function (call: ApiCall<ReqCheckOpen, ResCheckOpen>) {
    let logs = await PayFun.getPayLogs(call.uid);

    let data = await G.mongodb.collection('event').find({ uid: call.uid, type: { $in: type}}).toArray();

    let record = {}
    for (const i of type) {
        let payId = i == 'tianshujijin' ? 'zhongshenka' : i;
        let isPay = logs?.[payId]?.length > 0

        if(!isPay) record[i] = !isPay
        else {
            let rec = data.filter(v=>v.type==i)[0]?.rec
            let popVal:any = rec && Object.values(rec).at(-1)
            record[i] = !(popVal && popVal.pt && popVal.tq);
            if(!record[i]){
                G.mongodb.collection('syncBtns').updateOne({ uid: call.uid }, { $set: G.mongodb.createTreeObj({ key: i, val: { active: false } }) });
            }
        }
    }

    call.succ(record);
}