<?php


namespace App\Classes\Modules\Transactions\ControllersLogic;

use App\Models\Booking;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use App\Http\Resources\TransactionResource;
use App\Classes\ValueObjects\Constants\DocumentType;
use App\Classes\Exceptions\MalformedRequestException;
use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Classes\ValueObjects\Constants\TransactionType;
use App\Classes\ValueObjects\Constants\PaymentMethodType;
use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Companies\Services\FetchesCompany;
use App\Classes\Modules\Transactions\Services\ListsGroups;
use App\Classes\Modules\Transactions\Services\FetchesGroup;
use App\Classes\Modules\Transactions\DataTransferObjects\TransactionObject;
use App\Classes\Modules\Transactions\Processors\CreateInvoiceDocumentProcessor;

class CreateBulkPurchaseOrderTransactionLogic extends AbstractControllerLogic
{

    /**
     * @return array
     */
    protected function notification():array {
        return [
            'title' => 'Generate Bulk Purchase Order',
            'message' => 'You have successfully generated bulk purchase order'
        ];
    }

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

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

    /** @var CreateInvoiceDocumentProcessor */
    private $invoiceDocumentProcessor;

    /**
     * CreateBulkPurchaseOrderTransactionLogic constructor.
     * @param ListsGroups $listsGroups
     * @param FetchesCompany $fetchesCompany
     * @param CreateInvoiceDocumentProcessor $invoiceDocumentProcessor
     */
    public function __construct(ListsGroups $listsGroups, FetchesCompany $fetchesCompany, CreateInvoiceDocumentProcessor $invoiceDocumentProcessor)
    {
        $this->listsGroups = $listsGroups;
        $this->fetchesCompany = $fetchesCompany;
        $this->invoiceDocumentProcessor = $invoiceDocumentProcessor;
    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws \App\Classes\Exceptions\MalformedRequestException
     */
    public function logic(Request $request) : JsonResponse
    {
        $groups = $this->listsGroups->execute(['issuer_id' => [$request->issuer_id], 'date_start' => $request->start_date, 'date_end' => $request->end_date]);

        foreach($groups as $group){
            foreach($group->transactions as $transaction){
                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');
                }
            }
        }

        foreach($groups as $group){
            foreach($group->transactions as $transaction){
                $completed_transactions = $transaction->owner()->owner()->transactions()->where('type', TransactionType::PURCHASE_ORDER)->where('status', '=', ApprovalStatus::APPROVED)->get();

                $purchaseOrder = $transaction->owner()->transactions()
                    ->where('type', TransactionType::PURCHASE_ORDER)
                    ->complete()
                    ->first();

                foreach($completed_transactions as $transaction){
                    $supplier = $this->fetchesCompany->execute(['id' => $transaction->receiver]);

                    $voucherRedemption = $transaction->voucherRedemption;

                    // purchase order
                    $this->invoiceDocumentProcessor->execute($transaction, $purchaseOrder, $supplier, DocumentType::PURCHASE_ORDER, $voucherRedemption);
                }
            }
        }

        return $this->response([]);

    }



}
