<?php

namespace App\Classes\Modules\Schedules\ControllersLogic;

use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Schedules\Services\CreatesSchedule;
use App\Classes\Modules\Schedules\Standards\Rules\CanCreateSchedule;
use App\Classes\Modules\Schedules\DataTransferObjects\ScheduleObject;
use App\Classes\Modules\Transports\Services\FetchesTransport;
use App\Http\Resources\ScheduleResource;
use ErrorException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class CreateScheduleLogic extends AbstractControllerLogic
{
    /**
     * @return array
     */
    protected function notification():array {
        return [
            'title' => 'Created Schedule',
            'message' => 'You have successfully created a new Schedule'
        ];
    }

    /** @var CanCreateSchedule */
    private $canCreateSchedule;

    /** @var FetchesTransport */
    private $fetchesTransport;

    /** @var CreatesSchedule */
    private $createsSchedule;

    /**
     * CreateScheduleLogic constructor.
     * @param CanCreateSchedule $canCreateSchedule
     * @param FetchesTransport $fetchesTransport
     * @param CreatesSchedule $createsSchedule
     */
    public function __construct(CanCreateSchedule $canCreateSchedule, FetchesTransport $fetchesTransport, CreatesSchedule $createsSchedule)
    {
        $this->canCreateSchedule = $canCreateSchedule;
        $this->fetchesTransport = $fetchesTransport;
        $this->createsSchedule = $createsSchedule;
    }

    /**
     * @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
    {
        $object = new ScheduleObject($request->input('etd'), $request->input('eta'), ApprovalStatus::PENDING_SUBMISSION);

        $this->canCreateSchedule->passes($object);

        $query = $this->createsSchedule->execute($this->fetchesTransport->execute(['id' => $request->input('transport_id')]), $object);

        return $this->resourceResponse(new ScheduleResource($query));
    }

}
