'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 LifeStyleCard from './life-style-card';
import OrderAnalysesPackageCard from './order-analyses-package-card';
import Recommendations from './recommendations';
import RecommendationsSkeleton from './recommendations-skeleton';
import { AnalysisResponses } from './types';

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

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

  if (analysisResponses?.length === 0) {
    return (
      <>
        <OrderAnalysesPackageCard />
        <Suspense fallback={<RecommendationsSkeleton amount={1} />}>
          <LifeStyleCard
            account={account}
            analysisResponses={analysisResponses}
          />
        </Suspense>
      </>
    );
  }

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

export default AIBlocks;
