import { getFlowExecCtxForRemoteFuncs } from "$lib/core/server.utils";
import { command, getRequestEvent, query } from "$app/server";
import {
    checkLinkValidityFlow,
    endLinkSessionFlow,
    prepareLinkFlow,
    resolveLinkFlow,
} from "./service";
import { tokenSchema } from "./data";

export const resolveLinkSQ = query(tokenSchema, async ({ token }) => {
    const event = getRequestEvent();
    const fctx = await getFlowExecCtxForRemoteFuncs(event.locals);
    return resolveLinkFlow(fctx, token);
});

export const prepareLinkSC = command(tokenSchema, async ({ token }) => {
    const event = getRequestEvent();
    const fctx = await getFlowExecCtxForRemoteFuncs(event.locals);
    return prepareLinkFlow(fctx, token);
});

export const checkLinkValiditySQ = query(tokenSchema, async ({ token }) => {
    const event = getRequestEvent();
    const fctx = await getFlowExecCtxForRemoteFuncs(event.locals);
    return checkLinkValidityFlow(fctx, token);
});

export const endLinkSessionSC = command(tokenSchema, async ({ token }) => {
    const event = getRequestEvent();
    const fctx = await getFlowExecCtxForRemoteFuncs(event.locals);
    return endLinkSessionFlow(fctx, token);
});
