import IntelligenceModule from '@/components/organization/IntelligenceModule.tsx';
import WebIntelSourcesList from '@/components/organization/WebIntelSourcesList.tsx';
import { Text } from '@/components/ui/Typography.tsx';
import { Stack } from '@/components/ui/layout';
import { useTranslation } from '@/hooks/useI18n.tsx';
import { WebIntelligenceResult } from '@/types.ts';
import React from 'react';

interface WebIntelTabProps {
  webIntelResult: WebIntelligenceResult | null;
  isFetchingWebIntel: boolean;
  webIntelError: string | null;
  handleFetchWebIntelligence: () => void;
}

const WebIntelTab: React.FC<WebIntelTabProps> = ({
  webIntelResult,
  isFetchingWebIntel,
  webIntelError,
  handleFetchWebIntelligence,
}) => {
  const { t } = useTranslation();

  return (
    <IntelligenceModule
      description={t('mapSidebar.details.webIntelDescription')}
      buttonText={t('mapSidebar.details.fetchWebIntelButton')}
      onAction={handleFetchWebIntelligence}
      isPending={isFetchingWebIntel}
      error={webIntelError}
    >
      {webIntelResult && (
        <Stack spacing="md" className="mt-4 text-left text-base max-h-72 overflow-y-auto">
          {webIntelResult.text && <Text>{webIntelResult.text}</Text>}
          {Array.isArray(webIntelResult.sources) && webIntelResult.sources.length > 0 && (
            <WebIntelSourcesList
              sources={webIntelResult.sources}
              title={t('mapSidebar.details.webIntelSources')}
            />
          )}
        </Stack>
      )}
    </IntelligenceModule>
  );
};

export default React.memo(WebIntelTab);
