<?php

declare(strict_types=1);

namespace External\PompoBundle\Attachment;

use External\PompoBundle\DataGo\Util\DataGoApi;
use KupShop\OrderingBundle\Attachment\BaseAttachment;
use KupShop\OrderingBundle\Util\Invoice\PdfGenerator;
use Query\Operator;

class InvoicePDFAttachment extends BaseAttachment
{
    /** @required */
    public PdfGenerator $pdfGenerator;

    /** @required */
    public DataGoApi $dataGoApi;

    protected static $name = '[pompo] Faktura';
    protected static $type = 'pompo_invoice';

    protected $filename = 'Faktura_{KOD}.pdf';

    public function getContent()
    {
        $dataGoId = sqlQueryBuilder()
            ->select('id_datago')
            ->from('datago_orders')
            ->where(Operator::equals(['id_order' => $this->order->id]))
            ->execute()->fetchOne();

        if (!$dataGoId) {
            return '';
        }

        if (!($invoice = $this->dataGoApi->getOrderInvoice($dataGoId))) {
            return '';
        }

        return $invoice;
    }
}
