// Copyright (c) 2026 PowerOn AG
// All rights reserved.
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('Diese Datei scheint beschädigt zu sein. Sie hat eine PDF-Erweiterung, enthält aber Textinhalte. Bitte laden Sie die Datei erneut hoch, falls möglich.')}
            </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}
    />
  );
}

