'use client';

import React, { useMemo } from 'react';

import { useTranslations } from 'next-intl';

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

import { CreatorAward } from '@/types/Award';

import CreatorAwardsPDFDocument from './CreatorAwardsPDFDocument';
import type { CreatorAwardsPdfLabels } from './CreatorAwardsPDFDocument';

type CreatorAwardsPdfButtonProps = {
  awards: CreatorAward[];
  creatorName: string;
  creatorProfession?: string | null;
};

const CreatorAwardsPdfButton: React.FC<CreatorAwardsPdfButtonProps> = ({
  awards,
  creatorName,
  creatorProfession,
}) => {
  const tCommon = useTranslations('common');
  const tPdf = useTranslations('pdf.creatorAwards');
  const labels = useMemo<CreatorAwardsPdfLabels>(
    () => ({
      otherGroupLabel: tPdf('otherAwardsGroup'),
      stateGroupLabel: tPdf('stateAwardsGroup'),
      title: tPdf('title'),
    }),
    [tPdf]
  );
  const { downloadPdf, isPdfLoading } = usePdfDownload();

  if (awards.length === 0) return null;

  const handleClick = () => {
    downloadPdf(
      <CreatorAwardsPDFDocument
        awards={awards}
        creatorName={creatorName}
        creatorProfession={creatorProfession}
        labels={labels}
      />,
      createPdfFileName(creatorName, labels.title)
    );
  };

  return (
    <PdfButton
      onClick={handleClick}
      isGenerating={isPdfLoading}
      variant="ghost"
      size="icon"
      generatingLabel={tCommon('generatePdf')}
      className="self-end hover:bg-transparent hover:text-mma-yellow [&_svg]:size-6 md:[&_svg]:size-7"
    />
  );
};

export default CreatorAwardsPdfButton;
