import { LeagueRulebookTemplate } from '@/templates/LeagueRulebookTemplate';
import { ServiceFactory } from '@/lib/services/ServiceFactory';
import { getWebsiteApiBaseUrl } from '@/lib/config/apiBaseUrl';
import { LeagueDetailPageViewModel } from '@/lib/view-models/LeagueDetailPageViewModel';

interface LeagueRulebookStaticProps {
  leagueId: string;
}

export default async function LeagueRulebookStatic({ leagueId }: LeagueRulebookStaticProps) {
  const serviceFactory = new ServiceFactory(getWebsiteApiBaseUrl());
  const leagueService = serviceFactory.createLeagueService();
  
  let viewModel: LeagueDetailPageViewModel | null = null;
  let loading = false;

  try {
    loading = true;
    const data = await leagueService.getLeagueDetailPageData(leagueId);
    if (data) {
      viewModel = data;
    }
  } catch (err) {
    console.error('Failed to load scoring config:', err);
  } finally {
    loading = false;
  }

  if (!viewModel && !loading) {
    return (
      <div className="text-center text-gray-400 py-12">
        Unable to load rulebook
      </div>
    );
  }

  return <LeagueRulebookTemplate viewModel={viewModel!} loading={loading} />;
}