<?php


namespace App\Classes\Modules\Transactions\ControllersLogic;

use App\Classes\General\Abstracts\AbstractControllerLogic;
use Illuminate\Http\Request;
use App\Classes\Modules\Companies\Services\FetchesCompany;
use App\Classes\Modules\Transactions\Services\FetchesTransaction;
use App\Classes\Modules\KeyValuePairs\Services\CreatesKeyValuePair;
use App\Classes\Modules\KeyValuePairs\Services\UpdatesKeyValuePair;
use App\Classes\Modules\KeyValuePairs\DataTransferObjects\KeyValuePairObject;
use App\Classes\Modules\Transactions\Standards\Rules\CanUpdateCreditNote;
use App\Classes\ValueObjects\Constants\KVPKey;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Log;

class UpdateCreditNoteOptLogic extends AbstractControllerLogic
{

    /**
     * @return array
     */
    protected function notification(): array
    {
        return [
            'title' => 'Updated Credit Note',
            'message' => 'You have successfully updated this credit note'
        ];
    }

    /** @var CanUpdateCreditNote */
    private $canUpdateCreditNote;

    /** @var FetchesTransaction */
    private $fetchesTransaction;

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

    /** @var CreatesKeyValuePair */
    private $createsKeyValuePair;

    /** @var UpdatesKeyValuePair */
    private $updatesKeyValuePair;

    /**
     * UpdateCreditNoteOptLogic constructor.
     * @param FetchesTransaction $fetchesTransaction
     * @param FetchesCompany $fetchesCompany
     * @param CreatesKeyValuePair $createsKeyValuePair
     * @param UpdatesKeyValuePair $updatesKeyValuePair
     * @param CanUpdateCreditNote $canUpdateCreditNote
     */
    public function __construct(FetchesTransaction $fetchesTransaction, FetchesCompany $fetchesCompany, CreatesKeyValuePair $createsKeyValuePair, UpdatesKeyValuePair $updatesKeyValuePair, CanUpdateCreditNote $canUpdateCreditNote)
    {
        $this->fetchesTransaction = $fetchesTransaction;
        $this->fetchesCompany = $fetchesCompany;
        $this->createsKeyValuePair = $createsKeyValuePair;
        $this->updatesKeyValuePair = $updatesKeyValuePair;
        $this->canUpdateCreditNote = $canUpdateCreditNote;
    }

    /**
     * @param Request $request
     * @return string|\Symfony\Component\HttpFoundation\Response
     * @throws \App\Classes\Exceptions\MalformedRequestException
     */
    public function logic(Request $request): JsonResponse
    {
        $this->canUpdateCreditNote->passes();

        $transaction = $this->fetchesTransaction->execute(['id' => $request->route('id')]);

        $kvp = $transaction->attributesKVP()->where('key', KVPKey::CREDIT_NOTE_NORMAL_OPT_IN_OVERRIDE)->latest()->first();

        $supplier = $this->fetchesCompany->execute(['id' => $transaction->receiver]);
        if($supplier->e_invoice === 1) {
            if($kvp && $kvp->value == 1) {
                $this->updateOrCreateKeyValuePair($transaction, KVPKey::CREDIT_NOTE_NORMAL_OPT_IN_OVERRIDE, 0);
            }
            else
            {
                $this->updateOrCreateKeyValuePair($transaction, KVPKey::CREDIT_NOTE_NORMAL_OPT_IN_OVERRIDE, 1);
            }
        }
        return $this->response([]);
    }

    private function updateOrCreateKeyValuePair($transaction, $key, $value)
    {
        $keyValuePairObject = new KeyValuePairObject($key, $value);
        $metadata = $transaction->attributesKVP()->where('key', $key)->first();

        if ($metadata) {
            $this->updatesKeyValuePair->execute($metadata, $keyValuePairObject);
        } else {
            $this->createsKeyValuePair->execute($transaction, $keyValuePairObject);
        }
    }
}
