<?php

namespace App\Classes\Modules\Documents\ControllersLogic;

use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Http\Resources\DocumentResource;

use App\Classes\General\Abstracts\AbstractControllerLogic;

use App\Classes\Modules\Documents\Services\FetchesDocument;

use App\Classes\Modules\Documents\Standards\Rules\CanUpdateDocument;
use App\Classes\Modules\Documents\Services\ApprovesDocument;
use App\Classes\Modules\Documents\DataTransferObjects\DocumentObject;

use ErrorException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;

class ApproveDocumentLogic extends AbstractControllerLogic
{

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

    /** @var CanUpdateDocument */
    private $canUpdateDocument;

    /** @var ApprovesDocument */
    private $approvesDocument;

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

    /**
     * UpdateStandardSegmentConstantLogic constructor.
     * @param CanUpdateDocument $canUpdateDocument
     * @param ApprovesDocument $approvesDocument
     * @param FetchesDocument $fetchesDocument
     */
    public function __construct(
        canUpdateDocument $canUpdateDocument, 
        ApprovesDocument $approvesDocument, 
        FetchesDocument $fetchesDocument
    )
    {
        $this->canUpdateDocument = $canUpdateDocument;
        $this->approvesDocument = $approvesDocument;
        $this->fetchesDocument = $fetchesDocument;
    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws ErrorException
     */
    public function logic(Request $request) : JsonResponse
    {
        try {

            $document = $this->fetchesDocument->execute(['id' => $request->route('id')]);

            $this->canUpdateDocument->passes($document);

            $document_query = $this->approvesDocument->execute($document);
           

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

        } catch (\Exception $exception){
            throw new ErrorException($exception->getMessage(), $exception->getCode());
        }
    }

}