<?php

namespace App\Classes\Modules\PackingLists\ControllersLogic\Containers;

use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Transports\Services\UpdatesDispatchDateTransport;
use App\Classes\Modules\PackingLists\Services\Containers\FetchesContainer;
use App\Http\Resources\ContainerResource;
use ErrorException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Carbon\Carbon;

class UpdateDispatchDateContainerLogic extends AbstractControllerLogic
{

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


    /** @var UpdatesDispatchDateTransport */
    private $updatesDispatchDateTransport;

    /** @var FetchesContainer */
    private $fetchesContainer;

    /**
     * UpdateDispatchDateContainerLogic constructor.
     * @param CanUpdateContainer $canUpdateContainer
     * @param UpdatesDispatchDateTransport $updatesDispatchDateTransport
     * @param FetchesContainer $fetchesContainer
     */
    public function __construct(UpdatesDispatchDateTransport $updatesDispatchDateTransport, FetchesContainer $fetchesContainer)
    {
        $this->updatesDispatchDateTransport = $updatesDispatchDateTransport;
        $this->fetchesContainer = $fetchesContainer;
    }


    /**
     * @param Request $request
     * @return JsonResponse
     * @throws ErrorException
     */
    public function logic(Request $request) : JsonResponse
    {
        $container = $this->fetchesContainer->execute(['id' => $request->route('id')]);
        $transport = $container->transports()->first();
        $query = $this->updatesDispatchDateTransport->execute($transport, Carbon::parse($request->input('dispatch_date')));
        return $this->resourceResponse(new ContainerResource($container));
    }

}
