<?php


namespace App\Classes\Modules\Transactions\ControllersLogic;

use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Documents\DataTransferObjects\DocumentObject;
use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Classes\ValueObjects\Constants\DocumentType;
use App\Classes\ValueObjects\Constants\TransactionType;
use App\Models\Order;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Mccarlosen\LaravelMpdf\Facades\LaravelMpdf;
use App\Classes\Modules\Documents\Services\CreatesDocument;
use App\Classes\Modules\Documents\Services\CreatesFiles;
use App\Classes\Modules\Transactions\Services\FetchesTransaction;
use App\Models\Document;

class RegenerateSingleShippingInvoiceTransactionLogic extends AbstractControllerLogic
{
    /**
     * @return array
     */
    protected function notification():array {
        return [
            'title' => 'Regenerate Shipping Invoice',
            'message' => 'You have successfully regenerated shipping invoice'
        ];
    }

    /** @var CreatesDocument */
    private $createsDocument;

    /** @var CreatesFiles */
    private $createsFiles;

    /** @var FetchesTransaction */
    private $fetchesTransaction;

    /**
     * @param CreatesDocument $createsDocument
     */
    public function __construct(CreatesDocument $createsDocument, CreatesFiles $createsFiles, FetchesTransaction $fetchesTransaction)
    {
        $this->createsDocument = $createsDocument;
        $this->createsFiles = $createsFiles;
        $this->fetchesTransaction = $fetchesTransaction;
    }

    public function logic(Request $request) : JsonResponse
    {
        $invoice = $this->fetchesTransaction->execute(['id' => $request->route('invoice_id')]);

        $invoice->documents()->delete();

        $transaction_invoice_pdf = LaravelMpdf::loadView('pages.pdfs.shipping_invoice', ['invoice_transaction' => $invoice]);

        $document_object = new DocumentObject(
            DocumentType::SHIPPING_INVOICE,
            [chunk_split('data:application/pdf;base64,'.base64_encode($transaction_invoice_pdf->output()))],
            '',
            ApprovalStatus::COMPLETED,
            'shipping_invoice'
        );

        $document =$this->createsDocument->execute($invoice, $document_object);

        $this->createsFiles->execute($document, $document_object);

        // dump($document);

        return $this->response([]);
    }

}
