import { createFileRoute } from "@tanstack/react-router";
import { tournamentQueries, useCurrentTournament } from "@/features/tournaments/queries";
import UpcomingTournament from "@/features/tournaments/components/upcoming-tournament";
import { ensureServerQueryData } from "@/lib/tanstack-query/utils/ensure";

export const Route = createFileRoute("/_authed/")({
  component: Home,
  beforeLoad: async ({ context }) => {
    const queryClient = context.queryClient;
    const tournament = await ensureServerQueryData(queryClient, tournamentQueries.current())

    return { tournament }
  },
  loader: ({ context }) => ({
    withPadding: true,
    header: {
      title: context.tournament.name || "FLXN"
    }
  }),
});

function Home() {
  const { data: tournament } = useCurrentTournament();

  if (!tournament.matches || tournament.matches.length !== 0) {
    return <UpcomingTournament tournament={tournament} />;
  }

  return <p>Started Tournament</p>
}
