<?php

namespace App\Classes\Modules\HelpMenu\ControllersLogic;


use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\HelpMenu\Services\ListsHelpMenuQuestions;
use App\Classes\Modules\HelpMenu\Services\FetchesUrl;
use App\Http\Resources\HelpMenuQuestionResource;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Crypt;
use App\Classes\Exceptions\ResourceNotFoundException;

class ListQuestionsQALogic extends AbstractControllerLogic
{

    /**
     * @return array
     */
    protected function notification():array {
        return [
            'title' => 'Retrieved Questions',
            'message' => 'You have successfully retrieved a list of Questions'
        ];
    }

    /** @var ListsHelpMenuQuestions */
    private $listsHelpMenuQuestions;

    /** @var FetchesUrl */
    private $fetchesUrl;

    /**
     * ListQuestionsQALogic constructor.
     * @param ListsHelpMenuQuestions $listsHelpMenuQuestions
     * @param FetchesUrl $fetchesUrl
     */
    public function __construct(ListsHelpMenuQuestions $listsHelpMenuQuestions, FetchesUrl $fetchesUrl)
    {
        $this->listsHelpMenuQuestions = $listsHelpMenuQuestions;
        $this->fetchesUrl = $fetchesUrl;
    }


    /**
     * @param Request $request
     * @return JsonResponse
     * @throws \App\Classes\Exceptions\AccessForbiddenException
     * @throws \App\Classes\Exceptions\MalformedRequestException
     * @throws \App\Classes\Exceptions\RequestValidationException
     * @throws \App\Classes\Exceptions\ResourceNotFoundException
     */
    public function logic(Request $request) : JsonResponse
    {
        $url = $this->fetchesUrl->execute(['short_url' => $request->input('token')]);
        $token = $url->original_url;
        $decriptedToken = Crypt::decryptString($token);
        $delimiter = "|";
        $parts = explode($delimiter, $decriptedToken);
        $questionSet = $parts[3];
        $query = $this->listsHelpMenuQuestions->execute(['questionnaire_set_id' => $questionSet]);
        return $this->collectionResponse(HelpMenuQuestionResource::collection($query));
    }

}
