<?php

namespace App\Classes\Modules\HelpMenu\ControllersLogic;

use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\PerfexCRM\Services\CreatesPerfexCRMSupportTicket;
use App\Classes\Modules\PerfexCRM\Services\FetchesPerfexCRMCustomer;
use App\Classes\Modules\HelpMenu\Standards\Rules\CanFetchHelpMenuQuestion;
use App\Classes\Modules\HelpMenu\Processors\SaveAnswerProcessor;
use App\Classes\Modules\HelpMenu\Processors\DeleteAnswerQAProcessor;
use App\Classes\Modules\HelpMenu\Processors\FetchNextQuestionQAProcessor;
use App\Classes\Modules\HelpMenu\Processors\FetchFirstQuestionQAProcessor;
use App\Http\Resources\HelpMenuQuestionResource;
use ErrorException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Auth;
use App\Classes\ValueObjects\Constants\QAType;

class UpdateNextQuestionQALogic extends AbstractControllerLogic
{

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

    /** @var CanFetchHelpMenuQuestion */
    private $canFetchHelpMenuQuestion;

    /** @var SaveAnswerProcessor */
    private $saveAnswerProcessor;

    /** @var DeleteAnswerQAProcessor */
    private $deleteAnswerQAProcessor;

    /** @var FetchNextQuestionQAProcessor */
    private $fetchNextQuestionQAProcessor;

    /** @var FetchFirstQuestionQAProcessor */
    private $fetchFirstQuestionQAProcessor;

    /** @var CreatesPerfexCRMSupportTicket */
    private $createsPerfexCRMSupportTicket;

    /** @var FetchesPerfexCRMCustomer */
    private $fetchesPerfexCRMCustomer;

    /**
     * UpdateNextQuestionQALogic constructor.
     * @param CanFetchHelpMenuQuestion $canFetchHelpMenuQuestion
     * @param SaveAnswerProcessor $saveAnswerProcessor
     * @param FetchNextQuestionQAProcessor $fetchNextQuestionQAProcessor
     * @param FetchFirstQuestionQAProcessor $fetchFirstQuestionQAProcessor
     * @param DeleteAnswerQAProcessor $deleteAnswerQAProcessor
     */
    public function __construct(CanFetchHelpMenuQuestion $canFetchHelpMenuQuestion, SaveAnswerProcessor $saveAnswerProcessor, FetchNextQuestionQAProcessor $fetchNextQuestionQAProcessor, FetchFirstQuestionQAProcessor $fetchFirstQuestionQAProcessor,  CreatesPerfexCRMSupportTicket $createsPerfexCRMSupportTicket, FetchesPerfexCRMCustomer $fetchesPerfexCRMCustomer, DeleteAnswerQAProcessor $deleteAnswerQAProcessor)
    {
        $this->canFetchHelpMenuQuestion = $canFetchHelpMenuQuestion;
        $this->saveAnswerProcessor = $saveAnswerProcessor;
        $this->fetchNextQuestionQAProcessor = $fetchNextQuestionQAProcessor;
        $this->fetchFirstQuestionQAProcessor = $fetchFirstQuestionQAProcessor;
        $this->createsPerfexCRMSupportTicket = $createsPerfexCRMSupportTicket;
        $this->fetchesPerfexCRMCustomer = $fetchesPerfexCRMCustomer;
        $this->deleteAnswerQAProcessor = $deleteAnswerQAProcessor;
    }


    /**
     * @param Request $request
     * @return JsonResponse
     * @throws MalformedRequestException
     * @throws ResourceNotFoundException
     */
    public function logic(Request $request) : JsonResponse
    {
        $questionId = 0;
        $userId = Auth::user()->id;
        $currentQuestion = $request->question;
        $questionId = $currentQuestion['id'];
        $questionType = $currentQuestion['question_type'];

        $this->canFetchHelpMenuQuestion->passes();

        //Delete answer
        $this->deleteAnswerQAProcessor->execute($userId, $questionId, $request);

        //Save answer given by user
        if (!$request->has('isPrevious')) {
            $filesUpload = $request->input('files');
            $answerOptionId = 0;
            if ($request->has('answerObj')) {
                $answerOptionId = intval($request->answerObj['id']);
            }
            $answerInText = $request->answer;
            $this->saveAnswerProcessor->execute($userId, 0, $questionId, $answerInText, $answerOptionId, $filesUpload);
        }

        //Create ticket at crm
        if (!$request->has('isPrevious')) {
            if($questionType == QAType::TICKET_CRM){
                $email = Auth::user()->email;
                $customer = $this->fetchesPerfexCRMCustomer->execute($email);
                $subject = "test subject 3";
                $department = 1;
                $contactid = 0;
                $userid = 0;
                if($customer){
                    $contactid = $customer->id;
                    $userid = $customer->userid;
                }
                $this->createsPerfexCRMSupportTicket->execute($subject, $department, $contactid, $userid);
            }
        }

        //Get returned question (previous or next)
        $returnQuestion = $this->fetchNextQuestionQAProcessor->execute($request);
        //When a questionnaire ended, return back the first question
        if(is_null($returnQuestion)){
            $returnQuestion = $this->fetchFirstQuestionQAProcessor->execute($request);
        }

        return $this->resourceResponse(new HelpMenuQuestionResource($returnQuestion, $userId));
    }
}
