<?php

namespace App\Classes\Modules\Schedules\ControllersLogic;


use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Schedules\Services\UpdatesSchedule;
use App\Classes\Modules\Schedules\Services\FetchesSchedule;
use App\Classes\Modules\Schedules\Standards\Rules\CanUpdateSchedule;
use App\Classes\Modules\Schedules\DataTransferObjects\ScheduleObject;
use App\Http\Resources\ScheduleResource;
use ErrorException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class UpdateScheduleLogic extends AbstractControllerLogic
{

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

    /** @var CanUpdateSchedule */
    private $canUpdateSchedule;

    /** @var UpdatesSchedule */
    private $updatesSchedule;

    /** @var FetchesSchedule */
    private $fetchesSchedule;


    /**
     * UpdateScheduleLogic constructor.
     * @param CanUpdateSchedule $canUpdateSchedule
     * @param UpdatesSchedule $updatesSchedule
     * @param FetchesSchedule $fetchesSchedule
     */
    public function __construct(CanUpdateSchedule $canUpdateSchedule, UpdatesSchedule $updatesSchedule, FetchesSchedule $fetchesSchedule)
    {
        $this->canUpdateSchedule = $canUpdateSchedule;
        $this->updatesSchedule = $updatesSchedule;
        $this->fetchesSchedule = $fetchesSchedule;
    }


    /**
     * @param Request $request
     * @return JsonResponse
     * @throws ErrorException
     */
    public function logic(Request $request) : JsonResponse
    {
        $query = $this->fetchesSchedule->execute(['id' => $request->route('id')]);

        $object = new ScheduleObject($request->input('etd'), $request->input('eta'), $query->owner_id, $request->input('status'));

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

        $query = $this->updatesSchedule->execute($query, $object);

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

}
