<?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;

class CreateStorageInvoiceDocTransactionProcessor
{

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

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

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


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


    /**
     * @throws MalformedRequestException
     */
    public function execute(PackingList $packingList)
    {
         /** @var Transaction $invoice_transaction */
         $invoice_transaction = $packingList->transactions()->where('transactions.type', TransactionType::STORAGE_INVOICE)->whereIn('status', [ApprovalStatus::COMPLETED])->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::STORAGE_INVOICE,
             [chunk_split('data:application/pdf;base64,'.base64_encode($transaction_invoice_pdf->output()))],
             '',
             ApprovalStatus::COMPLETED,
             'storage_invoice'
         );

        $invouce_transaction_document = $invoice_transaction->documents()->where('document_type', DocumentType::STORAGE_INVOICE)->first();
        if(!$invouce_transaction_document){
            /** @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));
            }
        }
    }

}
