<?php

namespace App\Classes\Modules\AddressBook\Services;


use App\Models\Company;
use App\Http\Resources\AddressBook as AddressBookResource;
use Illuminate\Http\Request;

class UpdatesAddressBook
{

    /** @var  Company */
    private $repository;

    /**
     * UpdatesAddressBook constructor.
     * @param Company $repository
     */
    public function __construct(Company $repository)
    {
        $this->repository = $repository;
    }


    public function execute(String $companyId, Request $request){

        $address = $this->repository->findOrfail($companyId)->addressBook()->findOrFail($request->input('id'));

        $address->reference_id = $companyId;
        $address->reference = $request->input('reference');
        $address->contact = $request->input('contact');
        $address->street_one = $request->input('street_one');
        $address->street_two = $request->input('street_two');
        $address->city = $request->input('city');
        $address->state = $request->input('state');
        $address->post_code =  $request->input('post_code');
        $address->country = $request->input('country');

        if($address->save()){
            return new AddressBookResource($address);
        }

    }

}