import { requireUserInServerComponent } from '@/lib/server/require-user-in-server-component';
import { createAccountsApi } from '@/packages/features/accounts/src/server/api';
import { getSupabaseServerClient } from '@/packages/supabase/src/clients/server-client';

import ConsentDialog from './(user)/_components/consent-dialog';
import { loadCurrentUserAccount } from './(user)/_lib/server/load-user-account';

export default async function HomeLayout({
  children,
}: {
  children: React.ReactNode;
}) {
  const client = getSupabaseServerClient();

  const user = await requireUserInServerComponent();
  const account = await loadCurrentUserAccount();
  const api = createAccountsApi(client);

  const hasAccountTeamMembership = await api.hasAccountTeamMembership(
    account?.id,
  );

  if (
    hasAccountTeamMembership &&
    account &&
    account?.has_consent_anonymized_company_statistics === null
  ) {
    return (
      <div className="container">
        <ConsentDialog userId={user.id} />
      </div>
    );
  }

  return <>{children}</>;
}
