<?php

namespace App\Classes\Modules\Transactions\Processors;


use App\Classes\Exceptions\MalformedRequestException;
use App\Classes\Notifications\InvoiceIssuedEmail;
use App\Models\Document;
use App\Models\PackingList;
use App\Models\Transaction;
use Mccarlosen\LaravelMpdf\Facades\LaravelMpdf;
use App\Classes\ValueObjects\Constants\DocumentType;
use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Classes\ValueObjects\Constants\TransactionType;
use App\Classes\Modules\Documents\Services\CreatesFiles;
use App\Classes\Modules\Documents\Services\CreatesDocument;
use App\Classes\Modules\Documents\DataTransferObjects\DocumentObject;
use App\Classes\Modules\Transactions\Services\UpdatesTransactionStatus;
use App\Classes\Jobs\CreatePerfexCRMInvoice;
use App\Classes\Modules\Transactions\Processors\CheckStorageInvoiceTransactionProcessor;
use App\Models\Order;

class ApproveShippingInvoiceTransactionProcessor
{

    /** @var UpdatesTransactionStatus */
    private $updatesTransactionStatus;

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

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

    /** @var CheckStorageInvoiceTransactionProcessor */
    private $storageInvoiceTransactionProcessor;


    /**
     * @param UpdatesTransactionStatus $updatesTransactionStatus
     * @param CreatesDocument $createsDocument
     * @param CreatesFiles $createsFiles
     */
    public function __construct(UpdatesTransactionStatus $updatesTransactionStatus, CreatesDocument $createsDocument, CreatesFiles $createsFiles, CheckStorageInvoiceTransactionProcessor $storageInvoiceTransactionProcessor)
    {
        $this->updatesTransactionStatus = $updatesTransactionStatus;
        $this->createsDocument = $createsDocument;
        $this->createsFiles = $createsFiles;
        $this->storageInvoiceTransactionProcessor = $storageInvoiceTransactionProcessor;
    }


    /**
     * @param PackingList $packingList
     * @throws MalformedRequestException
     */
    public function execute(PackingList $packingList)
    {
        /** @var Transaction $invoice_transaction */
        $invoice_transaction = $packingList->transactions()->where('transactions.type', TransactionType::SHIPPING_INVOICE)->whereIn('status', [ApprovalStatus::PENDING_VERIFICATION, ApprovalStatus::PENDING_SUBMISSION])->first();

        $this->updatesTransactionStatus->execute($invoice_transaction, ApprovalStatus::APPROVED);

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

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

        /** @var Document $document */
        $document = $this->createsDocument->execute($invoice_transaction, $document_object);

        $this->createsFiles->execute($document, $document_object);
        $user = $packingList->owner->companyModule->employees()->first();
        if(app()->environment(['production'])) {
            $user->notify(new InvoiceIssuedEmail($user, $packingList));
        }

        $order = $packingList->owner()->first();
        if($order instanceof Order){
            $this->storageInvoiceTransactionProcessor->executeOrder($order);
        }

        return;
    }

}
