<?php

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

use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Transports\Services\UpdatesDropDateTransport;
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 UpdateDropDateContainerLogic extends AbstractControllerLogic
{

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


    /** @var UpdatesDropDateTransport */
    private $updatesDropDateTransport;

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

    /**
     * UpdateDropDateContainerLogic constructor.
     * @param CanUpdateContainer $canUpdateContainer
     * @param UpdatesDropDateTransport $updatesDropDateTransport
     * @param FetchesContainer $fetchesContainer
     */
    public function __construct(UpdatesDropDateTransport $updatesDropDateTransport, FetchesContainer $fetchesContainer)
    {
        $this->updatesDropDateTransport = $updatesDropDateTransport;
        $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->updatesDropDateTransport->execute($transport, Carbon::parse($request->input('drop_date')));
        return $this->resourceResponse(new ContainerResource($container));
    }

}
