<?php

namespace MintyPHP\Service\AddressBook;

use MintyPHP\Service\CustomField\CustomFieldServicesFactory;
use MintyPHP\Service\Directory\DirectoryServicesFactory;
use MintyPHP\Service\Tenant\TenantScopeService;
use MintyPHP\Service\User\UserServicesFactory;

class AddressBookServicesFactory
{
    private ?AddressBookService $addressBookService = null;

    public function __construct(
        private readonly UserServicesFactory $userServicesFactory,
        private readonly DirectoryServicesFactory $directoryServicesFactory,
        private readonly CustomFieldServicesFactory $customFieldServicesFactory,
        private readonly TenantScopeService $tenantScopeService
    ) {
    }

    public function createAddressBookService(): AddressBookService
    {
        if ($this->addressBookService !== null) {
            return $this->addressBookService;
        }

        $userFactory = $this->userServicesFactory;
        $directoryFactory = $this->directoryServicesFactory;
        return $this->addressBookService = new AddressBookService(
            $userFactory->createUserAccountService(),
            $userFactory->createUserAssignmentService(),
            $directoryFactory->createTenantService(),
            $directoryFactory->createDepartmentService(),
            $directoryFactory->createRoleService(),
            $this->tenantScopeService,
            $this->customFieldServicesFactory->createUserCustomFieldValueService(),
            $userFactory->createUserAvatarService()
        );
    }
}
