import { t, publicProcedure, router } from "./trpc";
import prisma from "@/lib/db";
import { z } from "zod";

export const appRouter = router({
  ping: publicProcedure.query(async () => {
    return { hello: "world" };
  }),
  org: {
    bySlug: publicProcedure
      .input(z.object({ mode: z.string() }))
      .query(async ({ input: { mode } }) => {
        if (!mode) return { mail: "", name: "" };
        const res = await fetch(
          "https://api.edu.cocorobo.cn/edu/admin/selectorganize",
          {
            method: "POST",
            body: JSON.stringify({ mode }),
            headers: {
              "Content-Type": "application/json",
            },
          }
        );
        const resJson = await res.json();
        const { mail, name } = resJson?.[0]?.[0];
        return { mail, name };
      }),
  },
  flowModel: {
    byId: publicProcedure
      .input(
        z.object({
          multiAgentId: z.string(),
        })
      )
      .query(async ({ input: { multiAgentId } }) => {
        const record = await prisma.muti_agent_list.findUnique({
          where: { id: multiAgentId },
        });
        if (!record) return null;
        return { ...record, content: JSON.parse(record.content!) };
      }),
  },
  // ...
});

// Export type router type signature,
// NOT the router itself.
export type AppRouter = typeof appRouter;
