'use client';

import React, { useMemo } from 'react';

import PdfButton from '@/components/pdf/PdfButton';
import { createPdfFileName } from '@/components/pdf/pdfUtils';
import { usePdfDownload } from '@/components/pdf/usePdfDownload';

import CreatorTextPDFDocument from './CreatorTextPDFDocument';

type CreatorTextPdfButtonProps = {
  creatorName: string;
  creatorProfession?: string | null;
  sectionTitle: string;
  sectionSubtitle?: string | null;
  content: string;
};

const CreatorTextPdfButton: React.FC<CreatorTextPdfButtonProps> = ({
  creatorName,
  creatorProfession,
  sectionTitle,
  sectionSubtitle,
  content,
}) => {
  const { downloadPdf, isPdfLoading } = usePdfDownload();
  const sections = useMemo(
    () => [
      {
        title: sectionTitle,
        subtitle: sectionSubtitle,
        content,
      },
    ],
    [sectionTitle, sectionSubtitle, content]
  );

  const handleDownloadPDF = () =>
    downloadPdf(
      <CreatorTextPDFDocument
        creatorName={creatorName}
        creatorProfession={creatorProfession || ''}
        sections={sections}
      />,
      createPdfFileName(creatorName, sectionTitle)
    );

  return (
    <PdfButton
      onClick={handleDownloadPDF}
      isGenerating={isPdfLoading}
      variant="ghost"
      size="icon"
      className="absolute right-2 top-2 hover:bg-transparent hover:text-mma-yellow [&_svg]:size-6 md:[&_svg]:size-7"
    />
  );
};

export default CreatorTextPdfButton;
