import { CardSchema } from "../../../../../../client";
import ButtonCopy from "../../../../../../components/ButtonCopy/ButtonCopy.tsx";
import { ButtonCopyControlled } from "../../../../../../components/ButtonCopyControlled/ButtonCopyControlled.tsx";
import { getCurrentDateTimeForFilename } from "../../../../../../shared/lib/date.ts";
import FileSaver from "file-saver";

type Props = {
    card: CardSchema;
}

const PaymentLinkButton = ({ card }: Props) => {
    const billRequestPdfUrl = card?.billRequest?.pdfUrl || card?.group?.billRequest?.pdfUrl;

    if (billRequestPdfUrl) {
        return (
            <ButtonCopy
                onCopiedLabel={"Ссылка скопирована в буфер обмена"}
                value={billRequestPdfUrl}
            >
                Ссылка на оплату
            </ButtonCopy>
        );
    }

    return (
        <ButtonCopyControlled
            onCopyClick={() => {
                const date =
                    getCurrentDateTimeForFilename();
                FileSaver.saveAs(
                    `${import.meta.env.VITE_API_URL}/card/billing-document/${card.id}`,
                    `bill_${card.id}_${date}.pdf`,
                );
            }}
            copied={false}
            onCopiedLabel={"Ссылка скопирована в буфер обмена"}
        >
            Ссылка на оплату (PDF)
        </ButtonCopyControlled>
    );
}

export default PaymentLinkButton;
