<?php

namespace App\Classes\Modules\Documents\ControllersLogic;

use App\Classes\Modules\Documents\Standards\Rules\CanUpdateDocumentReference;
use App\Http\Resources\DocumentResource;
use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Documents\Services\FetchesDocument;
use App\Classes\Modules\Documents\Services\UpdatesDocumentReference;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class UpdateDocumentReferenceLogic extends AbstractControllerLogic
{

    /**
     * @return array
     */
    protected function notification():array {
        return [
            'title' => 'Update Document Reference',
            'message' => 'You have successfully updated the Document Reference'
        ];
    }

    /** @var CanUpdateDocumentReference*/
    private $canUpdateDocumentReference;

    /** @var UpdatesDocumentReference */
    private $updatesDocumentReference;

    /** @var FetchesDocument */
    private $fetchesDocument;


    /**
     * RejectDocumentLogic constructor.
     * @param CanApproveDocument $canApproveDocument
     * @param ApprovesDocument $approvesDocument
     * @param FetchesDocument $fetchesDocument
     */
    public function __construct(CanUpdateDocumentReference $canUpdateDocumentReference, UpdatesDocumentReference $updatesDocumentReference, FetchesDocument $fetchesDocument)
    {
        $this->canUpdateDocumentReference = $canUpdateDocumentReference;
        $this->updatesDocumentReference = $updatesDocumentReference;
        $this->fetchesDocument = $fetchesDocument;
    }

    /**
     * @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
    {
        $document = $this->fetchesDocument->execute(['id' => $request->route('id')]);

        $this->canUpdateDocumentReference->passes();

        $document_query = $this->updatesDocumentReference->execute($document, $request->input('identification_number'));

        return $this->resourceResponse(new DocumentResource($document_query));

    }

}