<?php

namespace App\Classes\Modules\Transactions\Processors;

use App\Classes\Modules\Companies\Services\FetchesCompany;
use App\Classes\Modules\Documents\DataTransferObjects\DocumentObject;
use App\Classes\Modules\Documents\Services\CreatesDocument;
use App\Classes\Modules\Documents\Services\CreatesFiles;
use App\Classes\Modules\Transactions\Services\ListsGroups;
use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Classes\ValueObjects\Constants\DocumentType;
use App\Classes\ValueObjects\Constants\TransactionType;
use App\Models\Document;
use App\Models\Group;
use App\Models\Transaction;
use Mccarlosen\LaravelMpdf\Facades\LaravelMpdf;

class GeneratesGroupTransactionsWhiteForm
{


    /** @var ListsGroups */
    private $listsGroups;

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

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

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

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


    public function execute(){
        $groups = Group::where('status',  ApprovalStatus::PENDING_SUBMISSION)->get();


        foreach ($groups as $group) {
            $pdf = LaravelMpdf::loadView('pages.pdfs.currency_vendor_order', ['transactions' => $group->transactions, 'transferFeeTransactions' => $group->transferFees, 'supplier' => $group->issuerCompany]);

            $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($group, $object);
            $this->createsFile->execute($document, $object);

            $group->status = ApprovalStatus::APPROVED;
            $group->save();

        }
    }
}