import { createOpenPetsClient } from "./index.js";
import { validateReaction, type OpenPetsReaction } from "./protocol.js";

const client = createOpenPetsClient();
const [command = "status", first, second] = process.argv.slice(2);

try {
  const result = command === "hello"
    ? await client.hello()
    : command === "status"
      ? await client.status()
      : command === "react"
        ? await client.react(validateReaction(first ?? "idle"))
        : command === "say"
          ? await client.say(first ?? "Working on it", second ? { reaction: validateReaction(second) as OpenPetsReaction } : undefined)
          : command === "invalid-token"
            ? await runInvalidTokenCheck()
            : await client.status();

  console.log(JSON.stringify(result, null, 2));
} catch (error) {
  console.error(error instanceof Error ? error.message : String(error));
  process.exitCode = 1;
}

async function runInvalidTokenCheck(): Promise<unknown> {
  const { readDiscoveryFile, sendRequest } = await import("./index.js");
  const discovery = readDiscoveryFile();
  return sendRequest({ ...discovery, token: "invalid-token-value" }, "hello", {});
}
