<?php

namespace MintyPHP\Service\AddressBook;

use MintyPHP\Service\Directory\DirectoryScopeGateway;
use MintyPHP\Service\Access\RoleService;
use MintyPHP\Service\Org\DepartmentService;
use MintyPHP\Service\Tenant\TenantService;

class AddressBookDirectoryGateway
{
    public function __construct(
        private readonly ?TenantService $tenantService = null,
        private readonly ?DepartmentService $departmentService = null,
        private readonly ?RoleService $roleService = null,
        private readonly ?DirectoryScopeGateway $scopeGateway = null
    ) {
    }

    public function listTenants(): array
    {
        return $this->tenantService()->list();
    }

    public function listDepartmentsByTenantIds(array $tenantIds): array
    {
        return $this->departmentService()->listByTenantIds($tenantIds);
    }

    public function listDepartments(): array
    {
        return $this->departmentService()->list();
    }

    public function listActiveRoles(): array
    {
        return $this->roleService()->listActive();
    }

    public function getUserTenantIds(int $userId): array
    {
        return $this->scopeGateway()->getUserTenantIds($userId);
    }

    public function isStrictScope(): bool
    {
        return $this->scopeGateway()->isStrict();
    }

    public function canAccessUser(int $viewerUserId, int $targetUserId): bool
    {
        return $this->scopeGateway()->canAccess('users', $targetUserId, $viewerUserId);
    }

    private function tenantService(): TenantService
    {
        return $this->tenantService ?? new TenantService();
    }

    private function departmentService(): DepartmentService
    {
        return $this->departmentService ?? new DepartmentService();
    }

    private function roleService(): RoleService
    {
        return $this->roleService ?? new RoleService();
    }

    private function scopeGateway(): DirectoryScopeGateway
    {
        return $this->scopeGateway ?? new DirectoryScopeGateway();
    }
}
