<?php

namespace App\Classes\Modules\Addresses\ControllersLogic;

use App\Http\Resources\AddressResource;
use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Addresses\Services\FetchesAddress;
use App\Classes\Modules\Addresses\Standards\Rules\CanUpdateAddress;
use App\Classes\Modules\Addresses\Services\ResetsAddressesDefault;
use App\Classes\Modules\Addresses\Services\SetsAddressToDefault;

use App\Models\Address;
use ErrorException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;


class SetAddressToDefaultLogic extends AbstractControllerLogic
{

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

    /** @var ResetsAddressesDefault */
    private $resetsAddressesDefault;

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

    /** @var SetsAddressToDefault*/
    private $setsAddressToDefault;

    /**
     * SetAddressToDefaultLogic constructor.
     * @param ResetsAddressesDefault $resetsAddressesDefault
     * @param FetchesAddress $fetchesAddress
     * @param SetsAddressToDefault $setsAddressToDefault
     */
    public function __construct(ResetsAddressesDefault $resetsAddressesDefault, FetchesAddress $fetchesAddress, SetsAddressToDefault $setsAddressToDefault)
    {
        $this->resetsAddressesDefault = $resetsAddressesDefault;
        $this->fetchesAddress = $fetchesAddress;
        $this->setsAddressToDefault = $setsAddressToDefault;
    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws \App\Classes\Exceptions\MalformedRequestException
     */
    public function logic(Request $request): JsonResponse
    {

        /** @var Address $address */

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

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

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