<?php

namespace App\Classes\Modules\AddressBook\Services;


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

class DefaultsAddressBook
{
    /** @var  Company */
    private $repository;

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

    public function execute(String $companyId, String $id){

        $this->repository->findOrfail($companyId)->addressBook()->update(['default' => 0]);

        $address = $this->repository->findOrfail($companyId)->addressBook()->findOrFail($id);
        $address->default = 1;

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

}