<?php

namespace App\Classes\Modules\Companies\ControllersLogic;


use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Companies\Services\FetchesCompany;
use App\Classes\Modules\Companies\Services\UpdatesCompanyModuleIsCreditTerm;
use App\Classes\Modules\Companies\Standards\Rules\CanUpdateCompanyModule;
use App\Classes\Modules\Transactions\Services\ListsTransactions;
use App\Classes\Modules\Transactions\Services\UpdatesTransactionIsWaived;
use App\Classes\Modules\Transactions\Processors\CheckStorageInvoiceTransactionProcessor;
use App\Classes\ValueObjects\Constants\TransactionType;
use App\Models\Order;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;

class UpdateCompanyCreditTermStatusLogic extends AbstractControllerLogic
{

    /**
     * @return array
     */
    protected function notification():array {
        return [
            'title' => 'Update Company Credit Term Status',
            'message' => 'You have successfully updated Company Credit Term Status'
        ];
    }

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

    /** @var UpdatesCompanyModuleIsCreditTerm */
    private $updatesCompanyModuleIsCreditTerm;

    /** @var CanUpdateCompanyModule */
    private $canUpdateCompanyModule;

    /** @var ListsTransactions */
    private $listsTransactions;

    /** @var UpdatesTransactionIsWaived */
    private $updatesTransactionIsWaived;

    /** @var CheckStorageInvoiceTransactionProcessor */
    private $storageInvoiceTransactionProcessor;



    /**
     * UpdateCompanyCreditTermStatusLogic constructor.
     * @param FetchesCompany $fetchesCompany
     * @param UpdatesCompanyModuleIsCreditTerm $updatesCompanyModuleIsCreditTerm
     * @param CanUpdateCompanyModule $canUpdateCompanyModule
     * @param UpdatesTransactionIsWaived $updatesTransactionIsWaived
     * @param ListsTransactions $listsTransactions
     * @param CheckStorageInvoiceTransactionProcessor $storageInvoiceTransactionProcessor
     */
    public function __construct(FetchesCompany $fetchesCompany, UpdatesCompanyModuleIsCreditTerm $updatesCompanyModuleIsCreditTerm, CanUpdateCompanyModule $canUpdateCompanyModule, UpdatesTransactionIsWaived $updatesTransactionIsWaived, ListsTransactions $listsTransactions, CheckStorageInvoiceTransactionProcessor $storageInvoiceTransactionProcessor)
    {
        $this->fetchesCompany = $fetchesCompany;
        $this->updatesCompanyModuleIsCreditTerm = $updatesCompanyModuleIsCreditTerm;
        $this->canUpdateCompanyModule = $canUpdateCompanyModule;
        $this->listsTransactions = $listsTransactions;
        $this->updatesTransactionIsWaived = $updatesTransactionIsWaived;
        $this->storageInvoiceTransactionProcessor = $storageInvoiceTransactionProcessor;
    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws \App\Classes\Exceptions\AccessForbiddenException
     * @throws \App\Classes\Exceptions\MalformedRequestException
     * @throws \App\Classes\Exceptions\RequestValidationException
     */
    public function logic(Request $request) : JsonResponse
    {
        $this->canUpdateCompanyModule->passes();

        $company = $this->fetchesCompany->execute(['id' => $request->route('id')]);
        $companyModule = $company->companyModules()->first();
        $isCreditTerm = $companyModule->connections()->first()->is_credit_term;

        if ($isCreditTerm == 1) {
            $isCreditTerm = 0;
        } else {
            $isCreditTerm = 1;
        }

        if($isCreditTerm === 1){
            //{"per_page":10,"order_by":{"column":"id","DESC":true},"status_in":[2],"receiver":190,"type_in":[1],"does_not_have_payment_status_in":[0,1],"does_not_have_groups":1,"check_for_storage_invoice":1}

            $filters = [
                'status_in' => [2],
                'receiver' =>  $companyModule->id,
                'type_in' => [TransactionType::STORAGE_INVOICE]
            ];

            $transactions = $this->listsTransactions->execute($filters);
            foreach($transactions as $transaction){
                $this->updatesTransactionIsWaived->execute($transaction);
                $packingList = $transaction->owner()->first();
                if($packingList){
                    $order = $packingList->owner()->first();
                    if($order instanceof Order){
                        $storages = $this->storageInvoiceTransactionProcessor->executeOrder($order);
                    }
                }
            }
        }

        $this->updatesCompanyModuleIsCreditTerm->execute($company->companyModules()->first()->connections()->first(), $isCreditTerm);
        Log::info(Auth::user()->email." updated credit term status for customer with id: " .$request->route('id'). " to status " . $isCreditTerm);

        $result = ['is_credit_term' => $isCreditTerm];
        return $this->response(['data' => $result]);
    }
}
