import fs from "fs-extra";
import path from "path";
import { PKG_ROOT } from "~/consts.js";
import { type Installer } from "~/installers/index.js";
import { addPackageDependency } from "~/utils/addPackageDependency.js";

export const trpcInstaller: Installer = ({ frontendDir, packages }) => {
  addPackageDependency({
    frontendDir,
    dependencies: [
      "@tanstack/react-query",
      "superjson",
      "@trpc/server",
      "@trpc/client",
      "@trpc/next",
      "@trpc/react-query",
    ],
    devMode: false,
  });

  // const usingAuth = packages?.nextAuth.inUse;
  // const usingPrisma = packages?.prisma.inUse;

  const extrasDir = path.join(PKG_ROOT, "template/extras");

  const apiHandlerSrc = path.join(extrasDir, "src/pages/api/trpc/[trpc].ts");
  const apiHandlerDest = path.join(frontendDir, "src/pages/api/trpc/[trpc].ts");

  const utilsSrc = path.join(extrasDir, "src/utils/api.ts");
  const utilsDest = path.join(frontendDir, "src/utils/api.ts");

  const trpcFile = "base.ts";
  // usingAuth && usingPrisma
  //   ? "with-auth-prisma.ts"
  //   : usingAuth
  //   ? "with-auth.ts"
  //   : usingPrisma
  //   ? "with-prisma.ts"
  //   : "base.ts";
  const trpcSrc = path.join(extrasDir, "src/server/api/trpc", trpcFile);
  const trpcDest = path.join(frontendDir, "src/server/api/trpc.ts");

  const rootRouterSrc = path.join(extrasDir, "src/server/api/root.ts");
  const rootRouterDest = path.join(frontendDir, "src/server/api/root.ts");

  const nextConfigSrc = path.join(extrasDir, "config/with-trpc.next.config.mjs");
  const nextConfigDest = path.join(frontendDir, "next.config.mjs");

  const exampleRouterFile = "base.ts";
  // usingAuth && usingPrisma
  //   ? "with-auth-prisma.ts"
  //   : usingAuth
  //   ? "with-auth.ts"
  //   : usingPrisma
  //   ? "with-prisma.ts"
  //   : "base.ts";

  const exampleRouterSrc = path.join(
    extrasDir,
    "src/server/api/routers/example",
    exampleRouterFile,
  );
  const exampleRouterDest = path.join(frontendDir, "src/server/api/routers/example.ts");

  // copy files
  fs.copySync(apiHandlerSrc, apiHandlerDest);
  fs.copySync(utilsSrc, utilsDest);
  fs.copySync(trpcSrc, trpcDest);
  fs.copySync(rootRouterSrc, rootRouterDest);
  fs.copySync(exampleRouterSrc, exampleRouterDest);
  fs.copySync(nextConfigSrc, nextConfigDest);
};
