import { superTokensFunctionMiddleware } from "@/utils/supertokens";
import { createServerFn } from "@tanstack/react-start";
import { z } from "zod";
import { Logger } from "@/lib/logger";
import brackets from "./utils";
import { BracketData } from "./types";
import { toServerResult } from "@/lib/tanstack-query/utils/to-server-result";

const logger = new Logger("Bracket Generation");

export const previewBracket = createServerFn()
  .validator(z.number())
  .middleware([superTokensFunctionMiddleware])
  .handler(async ({ data: teams }) =>
    toServerResult(async () => {
      logger.info("Generating bracket", teams);
      if (!Object.keys(brackets).includes(teams.toString()))
        throw Error("Bracket not available");
      return brackets[teams as keyof typeof brackets] as BracketData;
    })
  );
