<?php


namespace App\Classes\Modules\Transactions\ControllersLogic;

use App\Classes\Modules\Transactions\Processors\CreateSupplierTransactionProcessor;
use App\Models\Document;
use App\Models\Group;

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\Modules\Documents\Services\CreatesFiles;
use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Companies\Services\FetchesCompany;
use Meneses\LaravelLaravelMpdf\Facades\LaravelLaravelMpdf;
use App\Classes\Modules\Documents\Services\CreatesDocument;
use App\Classes\Modules\Documents\DataTransferObjects\DocumentObject;

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 CreateSupplierTransactionProcessor */
    private $createSupplierTransactionProcessor;

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

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

    /**
     * CreateSupplierTransactionLogic constructor.
     * @param FetchesCompany $fetchesCompany
     * @param CreateSupplierTransactionProcessor $createSupplierTransactionProcessor
     * @param CreatesDocument $createsDocument
     * @param CreatesFiles $createsFile
     */
    public function __construct(FetchesCompany $fetchesCompany, CreateSupplierTransactionProcessor $createSupplierTransactionProcessor, CreatesDocument $createsDocument, CreatesFiles $createsFile)
    {
        $this->fetchesCompany = $fetchesCompany;
        $this->createSupplierTransactionProcessor = $createSupplierTransactionProcessor;
        $this->createsDocument = $createsDocument;
        $this->createsFile = $createsFile;
    }

    public function logic(Request $request) : JsonResponse
    {

        $supplier = $this->fetchesCompany->execute(['id' => $request->route('id')]);

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

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

        $this->createSupplierTransactionProcessor->execute($supplier, $rate, $payments);

        if(!count($this->createSupplierTransactionProcessor->getBills())) return $this->response([]);

        $group = new Group();
        $group->save();

        $issuer = '';
        $receiver = '';
        $amount = 0;
        $original_amount = 0;
        $currency_id = 0;
        $original_currency_id = '';
        $currency_rate = '';
        $tax = 0;
        $service_charge = 0;

        foreach ($this->createSupplierTransactionProcessor->getBills() as $key => $row) {
            $group->transactions()->sync($row->id, false);
            $issuer = $row->issuer;
            $receiver = $row->receiver;
            $amount += $row->amount;
            $original_amount += $row->original_amount;
            $currency_id = $row->currency_id;
            $original_currency_id = $row->original_currency_id;
            $currency_rate = $row->currency_rate;
            $tax += $row->tax;
            $service_charge += $row->service_charge;
        }

        $group->issuer = $issuer;
        $group->receiver = $receiver;
        $group->amount = $amount;
        $group->original_amount = $original_amount;
        $group->currency_id = $currency_id;
        $group->original_currency_id = $original_currency_id;
        $group->currency_rate = $currency_rate;
        $group->tax = $tax;
        $group->service_charge = $service_charge;

        $group->update();

        $pdf = LaravelMpdf::loadView('pages.pdfs.currency_vendor_order', ['transactions' => $this->createSupplierTransactionProcessor->getBills(), 'transferFeeTransactions' => $this->createSupplierTransactionProcessor->getTransferTransactions(), 'supplier' => $supplier]);

        $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([]);
    }
}
