<?php

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

use App\Classes\Modules\PackingLists\DataTransferObjects\PackageObject;
use App\Classes\Modules\PackingLists\Services\Packages\CreatesPackage;
use App\Classes\Modules\PackingLists\Standards\Rules\Packages\CanCreatePackage;
use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Orders\Services\FetchesOrder;
use App\Classes\Modules\Companies\Services\FetchesCompany;
use App\Http\Resources\PackageResource;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use App\Classes\ValueObjects\Constants\PackageType;

class CreatePackageLogic extends AbstractControllerLogic
{

    /**
     * @return array
     */
    protected function notification():array {
        return [
            'title' => 'Created Package',
            'message' => 'You have successfully created a new Package'
        ];
    }

    /** @var CanCreatePackage */
    private $canCreatePackage;

    /** @var FetchesOrder */
    private $fetchesOrder;

    /** @var FetchesCompany */
    private $fetchesCompany;

    /** @var CreatesPackage */
    private $createsPackage;

    /**
     * CreatePackageLogic constructor.
     * @param CanCreatePackage $canCreatePackage
     * @param FetchesOrder $fetchesOrder
     * @param FetchesCompany $fetchesCompany
     * @param CreatesPackage $createsPackage
     */
    public function __construct(CanCreatePackage $canCreatePackage, FetchesOrder $fetchesOrder, FetchesCompany $fetchesCompany, CreatesPackage $createsPackage)
    {
        $this->canCreatePackage = $canCreatePackage;
        $this->fetchesOrder = $fetchesOrder;
        $this->fetchesCompany = $fetchesCompany;
        $this->createsPackage = $createsPackage;
    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws \App\Classes\Exceptions\AccessForbiddenException
     * @throws \App\Classes\Exceptions\RequestValidationException
     */
    public function logic(Request $request) : JsonResponse
    {
        
        $order = $this->fetchesOrder->execute(['id' => $request->input('order_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'),
            ApprovalStatus::PENDING_VERIFICATION);

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

        $query = $this->createsPackage->execute($object, $order->packingLists()->first());

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

    }

}
