import { createClient } from '@supabase/supabase-js';
import { notFound } from 'next/navigation';
import { SiteRenderer } from '@kit/site-builder/components';

interface Props { params: Promise<{ slug: string; page: string[] }> }

export default async function ClubSubPage({ params }: Props) {
  const { slug, page: pagePath } = await params;
  const pageSlug = pagePath.join('/');

  const supabase = createClient(
    process.env.NEXT_PUBLIC_SUPABASE_URL!,
    process.env.NEXT_PUBLIC_SUPABASE_PUBLIC_KEY!,
  );

  const { data: account } = await supabase.from('accounts').select('id').eq('slug', slug).single();
  if (!account) notFound();

  const { data: settings } = await supabase.from('site_settings').select('*').eq('account_id', account.id).eq('is_public', true).maybeSingle();
  if (!settings) notFound();

  const { data: sitePageData } = await supabase.from('site_pages').select('*')
    .eq('account_id', account.id).eq('slug', pageSlug).eq('is_published', true).maybeSingle();
  if (!sitePageData) notFound();

  return (
    <div style={{ '--primary': settings.primary_color, fontFamily: settings.font_family } as React.CSSProperties}>
      <SiteRenderer data={(sitePageData.puck_data ?? {}) as Record<string, unknown>} />
    </div>
  );
}
