<?php

namespace App\Classes\Modules\Addresses\ControllersLogic;

use App\Classes\General\Interfaces\Addressable;
use App\Http\Resources\AddressResource;
use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Addresses\Services\FetchesAddress;
use App\Classes\Modules\Addresses\Services\ResetsAddressesDefault;
use App\Classes\Modules\Addresses\Services\SetsAddressToDefault;
use App\Models\Address;
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 FetchesAddress */
    private $fetchesAddress;

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

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


    /**
     * SetAddressToDefaultLogic constructor.
     * @param FetchesAddress $fetchesAddress
     * @param ResetsAddressesDefault $resetsAddressesDefault
     * @param SetsAddressToDefault $setsAddressToDefault
     */
    public function __construct(FetchesAddress $fetchesAddress, ResetsAddressesDefault $resetsAddressesDefault, SetsAddressToDefault $setsAddressToDefault)
    {
        $this->fetchesAddress = $fetchesAddress;
        $this->resetsAddressesDefault = $resetsAddressesDefault;
        $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->owner, $address->type);

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

    }
}
