<?php

namespace App\Classes\Modules\Documents\ControllersLogic;


use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Jobs\Services\CreatesJobResult;
use App\Classes\Modules\Jobs\DataTransferObjects\ListGenericJobObject;
use App\Classes\Jobs\ListDocumentsJob;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class ListDocumentJobLogic extends AbstractControllerLogic
{

    /**
     * @return array
     */
    protected function notification():array {
        return [
            'title' => 'List Document Job',
            'message' => 'You have successfully submit a job to list documents'
        ];
    }

    /** @var CreatesJobResult */
    private $createsJobResult;

    /**
     * ListDocumentJobLogic constructor.
     * @param CreatesJobResult $createsJobResult
     */
    public function __construct(CreatesJobResult $createsJobResult)
    {
        $this->createsJobResult = $createsJobResult;
    }


    /**
     * @param Request $request
     * @return JsonResponse
     */
    public function logic(Request $request) : JsonResponse
    {
        $jobId = uniqid();

        $user = Auth::user();
        $userInfo = (object) [
            'email' => $user->email,
            'type' => $user->type,
        ];

        $userInfoJson = json_encode($userInfo);
        $requestSignature = md5($userInfoJson . $request->fullUrl());

        $listGenericJobObject = new ListGenericJobObject(
            $request->fullUrl(),
            $request->all(),
            $requestSignature,
            null,
            $jobId,
            $userInfo
        );

        ListDocumentsJob::dispatch($listGenericJobObject)->onQueue('high_priority');

        $result = [];
        $result['job_id'] = $jobId;

        $this->createsJobResult->execute($listGenericJobObject);

        return $this->response(['data' => $result]);
    }
}
