<?php

namespace App\Classes\Modules\Addresses\ControllersLogic;


use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Addresses\Services\CreatesAddress;
use App\Classes\Modules\Addresses\Standards\Rules\CanCreateAddress;
use App\Classes\Modules\Addresses\DataTransferObjects\AddressObject;
use App\Http\Resources\AddressResource;
use ErrorException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class CreateAddressLogic extends AbstractControllerLogic
{

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

    /** @var CanCreateAddress */
    private $canCreateAddress;

    /** @var CreatesAddress */
    private $createsAddress;

    /**
     * CreateAddressControllerLogic constructor.
     * @param CanCreateAddress $canCreateAddress
     * @param CreatesAddress $createsAddress
     */
    public function __construct(CanCreateAddress $canCreateAddress, CreatesAddress $createsAddress)
    {
        $this->canCreateAddress = $canCreateAddress;
        $this->createsAddress = $createsAddress;
    }


    /**
     * @param Request $request
     * @return JsonResponse
     * @throws ErrorException
     */
    public function logic(Request $request) : JsonResponse
    {
        try {

            $object = new AddressObject($request->input('company_id'), $request->input('street_one'), $request->input('street_two'), $request->input('city'), $request->input('state'), $request->input('post_code'), $request->input('country'), $request->input('default'), $request->input('billing'));

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

            $query = $this->createsAddress->execute($object);

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

        } catch (\Exception $exception){
            throw new ErrorException($exception->getMessage(), $exception->getCode());
        }

    }

}