<?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\Services\FetchesAddress;
use App\Classes\Modules\Addresses\Services\ResetsAddressDefault;
use App\Classes\Modules\Addresses\Services\SetsAddressDefault;
use App\Classes\Modules\Addresses\Standards\Rules\CanCreateAddress;
use App\Classes\Modules\Addresses\DataTransferObjects\AddressObject;
use App\Http\Resources\AddressResource;
use App\Models\Address;
use ErrorException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class SetAddressDefaultLogic extends AbstractControllerLogic
{
    /**
     * SetAddressDefaultLogic constructor.
     * @param FetchesAddress $fetchesAddress
     * @param ResetsAddressDefault $resetsAddressDefault
     * @param SetsAddressDefault $setsAddressDefault
     */
    public function __construct(FetchesAddress $fetchesAddress, ResetsAddressDefault $resetsAddressDefault, SetsAddressDefault $setsAddressDefault)
    {
        $this->fetchesAddress = $fetchesAddress;
        $this->resetsAddressDefault = $resetsAddressDefault;
        $this->setsAddressDefault = $setsAddressDefault;
    }


    /**
     * @return array
     */
    protected function notification():array {
        return [
            'title' => 'Set Default Address',
            'message' => 'You have successfully updated your default address'
        ];
    }

    /** @var FetchesAddress */
    private $fetchesAddress;

    /** @var ResetsAddressDefault */
    private $resetsAddressDefault;

    /** @var SetsAddressDefault */
    private $setsAddressDefault;



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

            $address = $this->fetchesAddress->execute(['id' => $request->route('id')]);

            $this->resetsAddressDefault->execute($address);

            $this->setsAddressDefault->execute($address);

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

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

    }

}