<?php

namespace App\Classes\Modules\PackingLists\ControllersLogic;

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

class UpdateDispatchDatePackingListLogic extends AbstractControllerLogic
{

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


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

    /** @var FetchesPackingList */
    private $fetchesPackingList;

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


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

}
