<?php

namespace App\Classes\Modules\Addresses\ControllersLogic;


use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Addresses\Services\UpdatesAddress;
use App\Classes\Modules\Addresses\Services\FetchesAddress;
use App\Classes\Modules\Addresses\Services\FetchesDistrict;
use App\Classes\Modules\Addresses\Standards\Rules\CanUpdateAddress;
use App\Classes\Modules\Addresses\DataTransferObjects\AddressObject;
use App\Classes\Modules\Contacts\DataTransferObjects\ContactObject;
use App\Classes\Modules\Contacts\Processors\CreateContactProcessor;
use App\Classes\Modules\Remarks\DataTransferObjects\RemarkObject;
use App\Classes\Modules\Remarks\Processors\CreateRemarkProcessor;
use App\Http\Resources\AddressResource;
use App\Models\Address;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class UpdateAddressLogic extends AbstractControllerLogic
{

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

    /** @var CanUpdateAddress */
    private $canUpdateAddress;

    /** @var UpdatesAddress */
    private $updatesAddress;

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

    /** @var FetchesDistrict */
    private $fetchesDistrict;

    /** @var CreateContactProcessor */
    private $createContactProcessor;

    /** @var CreateRemarkProcessor */
    private $createRemarkProcessor;

    /**
     * UpdateAddressLogic constructor.
     * @param CanUpdateAddress $canUpdateAddress
     * @param UpdatesAddress $updatesAddress
     * @param FetchesAddress $fetchesAddress
     * @param FetchesDistrict $fetchesDistrict
     * @param CreateContactProcessor $createContactProcessor
     * @param CreateRemarkProcessor $createRemarkProcessor
     */
    public function __construct(CanUpdateAddress $canUpdateAddress, UpdatesAddress $updatesAddress, FetchesAddress $fetchesAddress, FetchesDistrict $fetchesDistrict, CreateContactProcessor $createContactProcessor, CreateRemarkProcessor $createRemarkProcessor)
    {
        $this->canUpdateAddress = $canUpdateAddress;
        $this->updatesAddress = $updatesAddress;
        $this->fetchesAddress = $fetchesAddress;
        $this->fetchesDistrict = $fetchesDistrict;
        $this->createContactProcessor = $createContactProcessor;
        $this->createRemarkProcessor = $createRemarkProcessor;
    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws \App\Classes\Exceptions\AccessForbiddenException
     * @throws \App\Classes\Exceptions\MalformedRequestException
     * @throws \App\Classes\Exceptions\RequestValidationException
     */
    public function logic(Request $request) : JsonResponse
    {
        $district = $this->fetchesDistrict->execute(['id' => $request->input('district_id')]);

        $object = new AddressObject($request->input('street_one'), $request->input('street_two'), $district->country_id, $district->state_id, $district->id, $request->input('post_code'), $request->input('reference'));

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

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

        /** @var Address $address */
        $address = $this->updatesAddress->execute($address, $object);

        $address->contacts()->delete(); $address->remarks()->delete();
dd($request->all());
        $contactObject = new ContactObject($request->input('person_in_charge'), $request->input('phone'), '', '');

        if($contactObject->getPhone()){
            $this->createContactProcessor->execute($contactObject, $address);
        }

        $remarkObject = new RemarkObject($request->input('remark'), Auth()->user()->id);

        if($remarkObject->getContent()){
            $this->createRemarkProcessor->execute($address, $remarkObject);
        }

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

    }

}