<?php


namespace App\Classes\Modules\Transactions\ControllersLogic;

use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Companies\Services\FetchesCompany;
use App\Classes\Modules\Transactions\DataTransferObjects\TransactionObject;
use App\Classes\Modules\Documents\DataTransferObjects\DocumentObject;
use App\Classes\Modules\Transactions\Services\CreatesTransaction;
use App\Classes\Modules\Transactions\Services\FetchesTransaction;
use App\Classes\Modules\Transactions\Services\GeneratesTransactionBillNumber;
use App\Classes\Modules\Transactions\Services\UpdatesTransactionStatus;
use App\Classes\Modules\Documents\Services\CreatesDocument;
use App\Classes\Modules\Documents\Services\CreatesFiles;
use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Classes\ValueObjects\Constants\PaymentMethodType;
use App\Classes\ValueObjects\Constants\TransactionType;
use App\Classes\ValueObjects\Constants\DocumentType;
use App\Models\Document;
use App\Models\Transaction;
use Barryvdh\DomPDF\PDF;
use Carbon\Carbon;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;

use Meneses\LaravelLaravelMpdf\Facades\LaravelLaravelMpdf;
use Meneses\LaravelMpdf\Facades\LaravelMpdf;

class CreateSupplierTransactionLogic extends AbstractControllerLogic
{


    /**
     * @return array
     */
    protected function notification():array {
        return [
            'title' => 'Create Supplier Transactions',
            'message' => 'You have successfully created currency supplier transactions'
        ];
    }

    /** @var FetchesCompany */
    private $fetchesCompany;

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

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

    /** @var CreatesTransaction */
    private $createsTransaction;

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

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

    /** @var GeneratesTransactionBillNumber */
    private $generatesTransactionBillNumber;

    /** @var PDF */
    private $pdf;

    /**
     * CreateSupplierTransactionLogic constructor.
     * @param FetchesCompany $fetchesCompany
     * @param FetchesTransaction $fetchesTransaction
     * @param UpdatesTransactionStatus $updatesTransactionStatus
     * @param CreatesTransaction $createsTransaction
     * @param GeneratesTransactionBillNumber $generatesTransactionBillNumber
     * @param PDF $pdf
     */
    public function __construct(FetchesCompany $fetchesCompany, FetchesTransaction $fetchesTransaction, UpdatesTransactionStatus $updatesTransactionStatus, CreatesTransaction $createsTransaction, CreatesDocument $createsDocument, CreatesFiles $createsFile, GeneratesTransactionBillNumber $generatesTransactionBillNumber, PDF $pdf)
    {

        $this->fetchesCompany = $fetchesCompany;
        $this->fetchesTransaction = $fetchesTransaction;
        $this->updatesTransactionStatus = $updatesTransactionStatus;
        $this->createsTransaction = $createsTransaction;
        $this->createsDocument = $createsDocument;
        $this->createsFile = $createsFile;
        $this->generatesTransactionBillNumber = $generatesTransactionBillNumber;
        $this->pdf = $pdf;
    }

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

        $rate =  $request->input('rate');

        $transactions = collect();

        foreach($request->input('payments') as $payment){

            /** @var Transaction $payment */
            $payment = $this->fetchesTransaction->execute(['id' => $payment['id']]);
            $this->updatesTransactionStatus->execute($payment, ApprovalStatus::COMPLETED);
            $billNumber = $this->generatesTransactionBillNumber->execute('SPLR-');
            $object = new TransactionObject($billNumber, TransactionType::BILL, $supplier->id, 1,
                $supplier->banks()->where('default', true)->first()->id, PaymentMethodType::CASH,
                $payment->original_amount * (1 / $rate), $payment->original_amount, 1, $payment->original_currency_id,
                $rate, 0, 0, null, ApprovalStatus::APPROVED);

            $transactions[] = $this->createsTransaction->execute($payment->booking, $object);
        }

        $pdf = LaravelMpdf::loadView('pages.pdfs.currency_vendor_order', ['transactions' => $transactions, 'supplier' => $supplier]);

        $path = Str::studly($supplier->name).'_'.Carbon::now()->format('Y_m_d_h_s_i').'.pdf';

        $object = new DocumentObject(
            DocumentType::CURRENCY_VENDOR_ORDER,
            [chunk_split('data:application/pdf;base64,'.base64_encode($pdf->output()))],
            '',
            ApprovalStatus::COMPLETED,
            'currency_vendor_order'
        );

        /** @var Document $document */
        $document = $this->createsDocument->execute($supplier, $object);
        $this->createsFile->execute($document, $object);

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