import { getAuth } from "@clerk/react-router/server";
import type { Route } from "./+types/api.admin.export-sports-data";
import { isUserAdminByClerkId } from "~/models/user";
import { exportSportsDataToJSON } from "~/utils/sports-data-sync.server";

export async function loader(args: Route.LoaderArgs) {
  const { userId } = await getAuth(args);

  if (!userId) {
    throw new Response("Unauthorized", { status: 401 });
  }

  const isAdmin = await isUserAdminByClerkId(userId);
  if (!isAdmin) {
    throw new Response("Forbidden", { status: 403 });
  }

  const data = await exportSportsDataToJSON();

  return Response.json(data);
}
