<?php

namespace App\Classes\Modules\Companies\ControllersLogic;


use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Companies\Services\UpdatesCompanyEInvoiceRequest;
use App\Classes\Modules\Companies\Services\FetchesCompany;
use App\Classes\Modules\Companies\DataTransferObjects\EInvoiceRequestDTO;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class UpdateCompanyEInvoiceRequestLogic extends AbstractControllerLogic
{

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

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

    /** @var UpdatesCompanyEInvoiceRequest */
    private $updatesCompanyEInvoiceRequest;


    /**
     * UpdateCompanyEInvoiceRequestLogic constructor.
     * @param FetchesCompany $fetchesCompany
     * @param UpdatesCompanyEInvoiceRequest $updatesCompanyEInvoiceRequest
     */
    public function __construct(FetchesCompany $fetchesCompany, UpdatesCompanyEInvoiceRequest $updatesCompanyEInvoiceRequest)
    {
        $this->fetchesCompany = $fetchesCompany;
        $this->updatesCompanyEInvoiceRequest = $updatesCompanyEInvoiceRequest;
    }

    /**
     * @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
    {
        $dto = new EInvoiceRequestDTO($request->all());
        $company = $this->fetchesCompany->execute(['id' => $dto->companyId]);
        $this->updatesCompanyEInvoiceRequest->execute($company, $dto->eInvoiceRequest);

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