'use server';

import React, { Suspense } from 'react';

import { AccountWithParams } from '@/packages/features/accounts/src/types/accounts';

import { isValidOpenAiEnv } from '../../_lib/server/is-valid-open-ai-env';
import { loadAnalyses } from '../../_lib/server/load-analyses';
import LifeStyleCard from './life-style-card';
import OrderAnalysesPackageCard from './order-analyses-package-card';
import Recommendations from './recommendations';
import RecommendationsSkeleton from './recommendations-skeleton';

const AIBlocks = async ({ account }: { account: AccountWithParams }) => {
  const isOpenAiAvailable = await isValidOpenAiEnv();

  if (!isOpenAiAvailable) {
    return <OrderAnalysesPackageCard />;
  }

  const { analyses, countryCode } = await loadAnalyses();

  if (analyses.length === 0) {
    return (
      <>
        <OrderAnalysesPackageCard />
        <Suspense fallback={<RecommendationsSkeleton />}>
          <LifeStyleCard />
        </Suspense>
      </>
    );
  }

  return (
    <Suspense fallback={<RecommendationsSkeleton />}>
      <LifeStyleCard />
      <Recommendations account={account} />
    </Suspense>
  );
};

export default AIBlocks;
