import { getLinkController } from "@pkg/logic/domains/link/controller";
import { createLinkSchema, updateLinkSchema } from "@pkg/logic/domains/link/data";
import { getFlowExecCtxForRemoteFuncs, unauthorized } from "$lib/core/server.utils";
import { command, getRequestEvent, query } from "$app/server";
import * as v from "valibot";

const lc = getLinkController();

export const listLinksSQ = query(async () => {
    const event = getRequestEvent();
    const fctx = await getFlowExecCtxForRemoteFuncs(event.locals);
    if (!fctx.userId) return unauthorized(fctx);

    const res = await lc.list(fctx);
    return res.isOk()
        ? { data: res.value, error: null }
        : { data: null, error: res.error };
});

export const getLinkByIdSQ = query(
    v.object({ id: v.number() }),
    async (input) => {
        const event = getRequestEvent();
        const fctx = await getFlowExecCtxForRemoteFuncs(event.locals);
        if (!fctx.userId) return unauthorized(fctx);

        const res = await lc.getById(fctx, input.id);
        return res.isOk()
            ? { data: res.value, error: null }
            : { data: null, error: res.error };
    },
);

export const createLinkSC = command(
    v.omit(createLinkSchema, ["token"]),
    async (payload) => {
        const event = getRequestEvent();
        const fctx = await getFlowExecCtxForRemoteFuncs(event.locals);
        if (!fctx.userId) return unauthorized(fctx);

        const res = await lc.create(fctx, payload);
        return res.isOk()
            ? { data: res.value, error: null }
            : { data: null, error: res.error };
    },
);

export const updateLinkSC = command(
    v.object({ id: v.number(), data: updateLinkSchema }),
    async (payload) => {
        const event = getRequestEvent();
        const fctx = await getFlowExecCtxForRemoteFuncs(event.locals);
        if (!fctx.userId) return unauthorized(fctx);

        const res = await lc.update(fctx, payload.id, payload.data);
        return res.isOk()
            ? { data: res.value, error: null }
            : { data: null, error: res.error };
    },
);

export const revokeLinkSC = command(
    v.object({ id: v.number() }),
    async (payload) => {
        const event = getRequestEvent();
        const fctx = await getFlowExecCtxForRemoteFuncs(event.locals);
        if (!fctx.userId) return unauthorized(fctx);

        const res = await lc.revoke(fctx, payload.id);
        return res.isOk()
            ? { data: res.value, error: null }
            : { data: null, error: res.error };
    },
);

export const deleteLinkSC = command(
    v.object({ id: v.number() }),
    async (payload) => {
        const event = getRequestEvent();
        const fctx = await getFlowExecCtxForRemoteFuncs(event.locals);
        if (!fctx.userId) return unauthorized(fctx);

        const res = await lc.delete(fctx, payload.id);
        return res.isOk()
            ? { data: res.value, error: null }
            : { data: null, error: res.error };
    },
);

export const assignDeviceSC = command(
    v.object({
        id: v.number(),
        deviceId: v.nullable(v.number()),
    }),
    async (payload) => {
        const event = getRequestEvent();
        const fctx = await getFlowExecCtxForRemoteFuncs(event.locals);
        if (!fctx.userId) return unauthorized(fctx);

        const res = await lc.assignDevice(fctx, payload.id, payload.deviceId);
        return res.isOk()
            ? { data: res.value, error: null }
            : { data: null, error: res.error };
    },
);
