import { RPCHandler } from "@orpc/server/fetch";

import { router } from "../rpc/index.js";
import { Hono } from "hono";

export const rpcApp = new Hono();

const handler = new RPCHandler(router);

rpcApp.all("/*", async (c) => {
  try {
    const { matched, response } = await handler.handle(c.req.raw, {
      prefix: "/rpc",
    });

    if (matched) {
      return c.newResponse(response.body, response);
    }

    return c.json({ error: "Not found" }, 404);
  } catch (error) {
    console.error("RPC Handler error:", error);
    
    // Let oRPC handle errors properly
    throw error;
  }
});
