<?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 GeneratesGroupTransactionsPurchaseOrder
{


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

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

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

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

    /**
     * GenerateGroupTransactionsPurchaseOrder 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::whereIn('status', [ApprovalStatus::PENDING_VERIFICATION, ApprovalStatus::APPROVED])->whereDoesntHave('transactions', function ($query){
            $query->whereHasMorph('owner', [Transaction::class], function($query){
                return $query->whereHas('booking', function($query){
                    return $query->whereDoesntHave('transactions', function($query){
                        return $query->where('type', TransactionType::PURCHASE_ORDER)->where('status', '=', ApprovalStatus::APPROVED);
                    });
                });
            });
        })->get();


        foreach ($groups as $group) {
//            if ($transaction->owner()->owner()->transactions()->where('type', TransactionType::PURCHASE_ORDER)->where('status', '!=', ApprovalStatus::APPROVED)->exists()) {
//                throw new MalformedRequestException('You can\'t generate bulk purchased order if there in uncomplete transactions');
//            }
            $supplier = $this->fetchesCompany->execute(['id' => $group->issuer]);

            $document_type = DocumentType::BULK_PURCHASE_ORDER;

            $lowercaseDocumentType = strtolower($document_type);

            $order_pdf = LaravelMpdf::loadView('pages.pdfs.bulk_purchase_order', ['group' => $group, 'supplier' => $supplier]);
            $document_object = new DocumentObject(
                $document_type,
                [chunk_split('data:application/pdf;base64,' . base64_encode($order_pdf->output()))],
                '',
                ApprovalStatus::COMPLETED,
                $lowercaseDocumentType . 's'
            );

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

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

        }
    }
}
