import React, { useCallback } from 'react';
import { HistoricalLandmark } from '@/types.ts';
import Button from '@/components/ui/Button.tsx';
import Spinner from '@/components/ui/Spinner.tsx';
import ErrorMessage from '@/components/ui/ErrorMessage.tsx';
import { Heading, Text } from '@/components/ui/Typography.tsx';
import { useTranslation } from '@/hooks/useI18n.tsx';
import { useGenerateHistoricalContext } from '@/hooks/useGemini.ts';

interface HistoricalContextAIProps {
  landmark: HistoricalLandmark;
}

const HistoricalContextAI = ({ landmark }: HistoricalContextAIProps) => {
  const { t } = useTranslation();
  const { data, error, isPending, refetch } = useGenerateHistoricalContext(landmark, t);

  const handleGenerate = useCallback(() => {
    refetch();
  }, [refetch]);

  return (
    <div>
      {!data && (
        <div className="text-center">
          <Text variant="muted" className="mb-2">{t('historicalContext.prompt')}</Text>
          <Button onClick={handleGenerate} disabled={isPending} size="sm">
            {isPending ? <Spinner className="h-4 w-4" /> : t('historicalContext.generateButton')}
          </Button>
        </div>
      )}

      {error && <ErrorMessage message={error.message} />}

      {data && (
        <div>
          <Heading level="h4" className="mb-2">{t('historicalContext.title')}</Heading>
          <Text as="div" className="border-l-4 border-primary pl-4 italic text-muted-foreground">
            {data}
          </Text>
        </div>
      )}
    </div>
  );
};

export default React.memo(HistoricalContextAI);
