<?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\Classes\Modules\Addresses\Services\FetchesAddress;
use App\Classes\Modules\Companies\Services\FetchesCompanyModule;
use App\Classes\ValueObjects\Constants\AddressType;
use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Http\Resources\AddressResource;
use App\Models\Address;
use App\Models\CompanyModule;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class CreateBillingAddressLogic extends AbstractControllerLogic
{

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

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

    /** @var FetchesCompanyModule */
    private $fetchesCompanyModule;

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

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

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

    /**
     * @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
    {
        $companyModule = $this->fetchesCompanyModule->execute(['id' => $request->input('company_module_id')]);
        
        $address = $this->fetchesAddress->execute(['id' => $request->input('billing_address_id')]);

        if ($address->type !== AddressType::BILLING || $address->id !== $companyModule->addresses->where('type', AddressType::BILLING)->first()->id) {
            // delete all current billing address
            Address::where('owner_type', CompanyModule::class)->where('owner_id', $request->input('company_module_id'))->where('type', AddressType::BILLING)->delete();

            // create addrress
            $billingAddress = new AddressObject($address->street_one, $address->street_two, $address->country_id, $address->state_id, $address->district_id, $address->postcode, AddressType::BILLING, ApprovalStatus::APPROVED, $address->reference);
            $this->canCreateAddress->passes($billingAddress);
            $this->createsAddress->execute($companyModule, $billingAddress);
        }

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

}