import { IoIosWarning } from 'react-icons/io';
import { useLanguage } from '../../../providers/language/LanguageContext';
import styles from '../ContentPreview.module.css';

interface PdfRendererProps {
  previewUrl?: string;
  previewContent?: string;
  fileName: string;
  onError: () => void;
}

export function PdfRenderer({ previewUrl, previewContent, fileName, onError }: PdfRendererProps) {
  const { t } = useLanguage();


  const handleLoad = () => {
    console.log('📄 PDF iframe loaded successfully:', { previewUrl, fileName });
  };

  const handleError = (event: React.SyntheticEvent<HTMLIFrameElement, Event>) => {
    console.error('❌ PDF iframe failed to load:', { previewUrl, fileName, event });
    onError();
  };

  // Handle corrupted PDF files (text content instead of PDF)
  if (previewContent && !previewUrl) {
    console.log('📄 Rendering corrupted PDF warning');
    return (
      <div className={styles.textContainer}>
        <div className={styles.textHeader}>
          <div className={styles.warningMessage}>
            <span className={styles.warningIcon}><IoIosWarning /></span>
            <span className={styles.warningText}>
              {t('files.preview.pdfFileCorrupted', 'This file appears to be corrupted. It has a PDF extension but contains text content. Please re-upload the file if possible.')}
            </span>
          </div>
        </div>
      </div>
    );
  }

  // Normal PDF rendering
  return (
    <iframe
      src={previewUrl}
      className={styles.previewIframe}
      title={`Preview of ${fileName}`}
      data-mime-type="application/pdf"
      onLoad={handleLoad}
      onError={handleError}
    />
  );
}

