<?php

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


use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\PackingLists\Services\Packages\UpdatesPackage;
use App\Classes\Modules\PackingLists\Services\Packages\FetchesPackage;
use App\Classes\Modules\PackingLists\Standards\Rules\Packages\CanUpdatePackage;
use App\Classes\Modules\PackingLists\DataTransferObjects\PackageObject;
use App\Http\Resources\PackageResource;
use ErrorException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class UpdatePackageLogic extends AbstractControllerLogic
{

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

    /** @var CanUpdatePackage */
    private $canUpdatePackage;

    /** @var UpdatesPackage */
    private $updatesPackage;

    /** @var FetchesPackage */
    private $fetchesPackage;


    /**
     * UpdatePackageLogic constructor.
     * @param CanUpdatePackage $canUpdatePackage
     * @param UpdatesPackage $updatesPackage
     * @param FetchesPackage $fetchesPackage
     */
    public function __construct(CanUpdatePackage $canUpdatePackage, UpdatesPackage $updatesPackage, FetchesPackage $fetchesPackage)
    {
        $this->canUpdatePackage = $canUpdatePackage;
        $this->updatesPackage = $updatesPackage;
        $this->fetchesPackage = $fetchesPackage;
    }


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

        $object = new PackageObject(
            $request->input('type'),
            $request->input('description'),
            $request->input('width'),
            $request->input('height'),
            $request->input('length'),
            $request->input('weight'),
            $request->input('quantity'),
            $request->input('status'),
            $package->reference
        );

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

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

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

}
