import { getDeviceController } from "@pkg/logic/domains/device/controller";
import {
    createDeviceSchema,
    deviceStatusSchema,
    updateDeviceSchema,
} from "@pkg/logic/domains/device/data";
import { getFlowExecCtxForRemoteFuncs, unauthorized } from "$lib/core/server.utils";
import { command, getRequestEvent, query } from "$app/server";
import * as v from "valibot";

const dc = getDeviceController();

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

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

export const getDeviceByIdSQ = 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 dc.getById(fctx, input.id);
        return res.isOk()
            ? { data: res.value, error: null }
            : { data: null, error: res.error };
    },
);

export const createDeviceSC = command(createDeviceSchema, async (payload) => {
    const event = getRequestEvent();
    const fctx = await getFlowExecCtxForRemoteFuncs(event.locals);
    if (!fctx.userId) return unauthorized(fctx);

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

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

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

export const deleteDeviceSC = 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 dc.delete(fctx, payload.id);
        return res.isOk()
            ? { data: res.value, error: null }
            : { data: null, error: res.error };
    },
);

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

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