<?php

namespace App\Classes\Modules\Transactions\ControllersLogic;


use App\Classes\Notifications\InvoiceIssuedEmail;
use Illuminate\Http\Request;


use Illuminate\Http\JsonResponse;
use Meneses\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\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Documents\Services\CreatesDocument;
use App\Classes\Modules\Documents\DataTransferObjects\DocumentObject;
use App\Classes\Modules\PackingLists\Services\FetchesPackingList;
use App\Classes\Modules\Transactions\Services\UpdatesTransactionStatus;
use App\Classes\Modules\Transactions\ControllersLogic\Document;

class ApproveShippingInvoiceTransactionLogic extends AbstractControllerLogic
{

    /**
     * @return array
     */
    protected function notification():array {
        return [
            'title' => 'Shipping Invoice Status',
            'message' => 'You have successfully updated the shipping invoice status'
        ];
    }

    /** @var FetchesPackingList */
    private $fetchesPackingList;

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

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

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

    /**
     * ApprovePaymentVerificationLogic constructor.
     * @param FetchesPackingList $fetchesPackingList
     * @param UpdatesTransactionStatus $updatesTransactionStatus
     * @param CreatesDocument $createsDocument
     * @param CreatesFiles $createsFiles
     * @param CreateInvoiceTransactionProcessor $createInvoiceTransactionProcessor
     */
    public function __construct(FetchesPackingList $fetchesPackingList, UpdatesTransactionStatus $updatesTransactionStatus, CreatesDocument $createsDocument, CreatesFiles $createsFiles)
    {
        $this->fetchesPackingList = $fetchesPackingList;
        $this->updatesTransactionStatus = $updatesTransactionStatus;
        $this->createsDocument = $createsDocument;
        $this->createsFiles = $createsFiles;
    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws \App\Classes\Exceptions\MalformedRequestException
     */
    public function logic(Request $request) : JsonResponse
    {
        $packing_list = $this->fetchesPackingList->execute(['id' => $request->route('id')]);

        $invoice_transaction = $packing_list->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 = $packing_list->owner->companyModule->employees()->first();
        if(app()->environment(['production'])) {
            $user->notify(new InvoiceIssuedEmail($user, $packing_list));
        }
        return $this->response([]);
    }

}
