<?php

namespace App\Classes\Modules\Companies\ControllersLogic;

use App\Classes\Modules\Companies\Services\UpdatesCompanyStatus;
use App\Classes\Modules\Documents\Services\RejectsDocument;
use App\Classes\Modules\Documents\Standards\Rules\CanApproveDocument;
use App\Classes\Modules\Orders\Services\UpdatesOrdersStatus;
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\Services\ApprovesDocument;

use App\Models\Document;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class ApproveIdentificationDocumentLogic extends AbstractControllerLogic
{

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

    /** @var CanApproveDocument*/
    private $canApproveDocument;

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

    /** @var RejectsDocument */
    private $rejectsDocument;

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

    /** @var UpdatesCompanyStatus */
    private $updatesCompanyStatus;

    /** @var UpdatesOrdersStatus */
    private $updatesOrdersStatus;

    /**
     * ApproveIdentificationDocumentLogic constructor.
     * @param CanApproveDocument $canApproveDocument
     * @param ApprovesDocument $approvesDocument
     * @param RejectsDocument $rejectsDocument
     * @param FetchesDocument $fetchesDocument
     * @param UpdatesCompanyStatus $updatesCompanyStatus
     * @param UpdatesOrdersStatus $updatesOrdersStatus
     */
    public function __construct(CanApproveDocument $canApproveDocument, ApprovesDocument $approvesDocument, RejectsDocument $rejectsDocument, FetchesDocument $fetchesDocument, UpdatesCompanyStatus $updatesCompanyStatus, UpdatesOrdersStatus $updatesOrdersStatus)
    {
        $this->canApproveDocument = $canApproveDocument;
        $this->approvesDocument = $approvesDocument;
        $this->rejectsDocument = $rejectsDocument;
        $this->fetchesDocument = $fetchesDocument;
        $this->updatesCompanyStatus = $updatesCompanyStatus;
        $this->updatesOrdersStatus = $updatesOrdersStatus;
    }

    /**
     * @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
    {

        $status = $request->route('status');

        /** @var Document $document */
        $document = $this->fetchesDocument->execute(['id' => $request->route('document_id')]);

        $this->canApproveDocument->passes();

        $document = $status === 'approve' ? $this->approvesDocument->execute($document) : $this->rejectsDocument->execute($document);

        $this->updatesCompanyStatus->execute($document->owner, $status === 'approve' ? ApprovalStatus::APPROVED : ApprovalStatus::REJECTED);

        if($status === 'approve'){
            $orders = $this->updatesOrdersStatus->execute($document->owner, ApprovalStatus::APPROVED);
        }


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

    }

}