import { FastifyRequest, FastifyReply } from "fastify";
import { setActivity, initRPC } from "./rp.service";

let initialized = false;

export function init() {
    if (!initialized) {
        initRPC(process.env.DISCORD_CLIENT_ID!);
        initialized = true;
    }
}

export async function setRPC(request: FastifyRequest, reply: FastifyReply) {
    const {
        details,
        state,
        mode,
        startTimestamp,
        endTimestamp,
        paused
    } = request.body as {
        details?: string;
        state?: string;
        mode?: "watching" | "reading" | string;
        startTimestamp?: number;
        endTimestamp?: number;
        paused?: boolean;
    };

    setActivity({
        details,
        state,
        mode,
        startTimestamp,
        endTimestamp,
        paused
    });

    return reply.send({ ok: true });
}