<?php

namespace App\Classes\Modules\Companies\ControllersLogic;


use App\Classes\Exceptions\CriteriaNotFulfilledException;
use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Addresses\Services\CreatesAddress;
use App\Classes\Modules\Addresses\Services\FetchesDistrict;
use App\Classes\Modules\Addresses\Services\UpdatesAddressMetadata;
use App\Classes\Modules\Addresses\Standards\Rules\CanCreateAddress;
use App\Classes\Modules\Addresses\DataTransferObjects\AddressObject;
use App\Classes\Modules\Companies\Services\FetchesCompany;
use App\Classes\Modules\Companies\Services\UpdatesCompanyEInvoiceInfo;
use App\Classes\Modules\Companies\DataTransferObjects\EInvoiceInfoDTO;
use App\Classes\Modules\Rules\Services\RuleEvaluator;
use App\Classes\Modules\Rules\Standards\Rules\CanPassEInvoicePromptedRule;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class UpdateCompanyEInvoiceInfoLogic extends AbstractControllerLogic
{

    /**
     * @return array
     */
    protected function notification():array {
        return [
            'title' => 'EInvoice Info Update',
            'message' => 'You have successfully updated company E-Invoice information'
        ];
    }

    /** @var CanCreateAddress */
    private $canCreateAddress;

    /** @var FetchesDistrict */
    private $fetchesDistrict;

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

    /** @var CreatesAddress */
    private $createsAddress;

    /** @var RuleEvaluator */
    private $ruleEvaluator;

    /** @var UpdatesCompanyEInvoiceInfo */
    private $updatesCompanyEInvoiceInfo;

    /** @var UpdatesAddressMetadata */
    private $updatesAddressMetadata;

    /** @var CanPassEInvoicePromptedRule */
    private $canPassEInvoicePromptedRule;

    /**
     * UpdateCompanyEInvoiceInfoLogic constructor.
     * @param CanCreateAddress $canCreateAddress
     * @param FetchesDistrict $fetchesDistrict
     * @param FetchesCompany $fetchesCompany
     * @param CreatesAddress $createsAddress
     * @param RuleEvaluator $ruleEvaluator;
     * @param UpdatesCompanyEInvoiceInfo $updatesCompanyEInvoiceInfo;
     * @param UpdatesAddressMetadata $updatesAddressMetadata
     * @param CanPassEInvoicePromptedRule $canPassEInvoicePromptedRule
     */
    public function __construct(CanCreateAddress $canCreateAddress, FetchesDistrict $fetchesDistrict, FetchesCompany $fetchesCompany, CreatesAddress $createsAddress, RuleEvaluator $ruleEvaluator, UpdatesCompanyEInvoiceInfo $updatesCompanyEInvoiceInfo, UpdatesAddressMetadata $updatesAddressMetadata, CanPassEInvoicePromptedRule $canPassEInvoicePromptedRule)
    {
        $this->canCreateAddress = $canCreateAddress;
        $this->fetchesDistrict = $fetchesDistrict;
        $this->fetchesCompany = $fetchesCompany;
        $this->createsAddress = $createsAddress;
        $this->ruleEvaluator = $ruleEvaluator;
        $this->updatesCompanyEInvoiceInfo = $updatesCompanyEInvoiceInfo;
        $this->updatesAddressMetadata = $updatesAddressMetadata;
        $this->canPassEInvoicePromptedRule = $canPassEInvoicePromptedRule;
    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws \App\Classes\Exceptions\AccessForbiddenException
     * @throws \App\Classes\Exceptions\MalformedRequestException
     * @throws \App\Classes\Exceptions\RequestValidationException
     * @throws \App\Classes\Exceptions\CriteriaNotFulfilledException
     */
    public function logic(Request $request) : JsonResponse
    {
        $dto = new EInvoiceInfoDTO($request->all());
        $result = $this->ruleEvaluator->evaluate([
            $this->canPassEInvoicePromptedRule,
        ], $dto);

        if ($result->failed()) {
            throw new CriteriaNotFulfilledException("- " . implode("<br>- ", $result->messages()));
        }

        $district = $this->fetchesDistrict->execute(['id' => $dto->districtId]);
        $object = new AddressObject($dto->streetOne, $dto->streetTwo, $district->country_id, $dto->stateId, $district->id, $dto->postCode);

        //Update Address
        $this->canCreateAddress->passes($object);
        $company = $this->fetchesCompany->execute(['id' => $dto->companyId]);
        $address = $this->createsAddress->execute($company, $object);
        $query = $this->updatesAddressMetadata->execute($address, false, true);

        //Update tin, msic code
        $this->updatesCompanyEInvoiceInfo->execute($company, $dto->tin, $dto->msicCode);

        return $this->response([]);
    }
}
