/// <summary>
        /// Creates a new tenant representing a Marain service that tenants can enroll to use.
        /// </summary>
        /// <param name="tenantStore">The tenant store.</param>
        /// <param name="manifest">
        /// The manifest for the service. The service name in the manifest must be unique across all service tenants.
        /// </param>
        /// <param name="logger">Optional logger.</param>
        /// <returns>The new tenant.</returns>
        public static async Task <ITenant> CreateServiceTenantAsync(this ITenantStore tenantStore, ServiceManifest manifest, ILogger?logger = null)
        {
            if (manifest == null)
            {
                throw new ArgumentNullException(nameof(manifest));
            }

            logger?.LogDebug("Validating service manifest for service creation.");

            await manifest.ValidateAndThrowAsync(tenantStore).ConfigureAwait(false);

            ITenant parent = await tenantStore.GetServiceTenantParentAsync().ConfigureAwait(false);

            logger?.LogDebug(
                "Creating new service tenant '{serviceName}' with well-known GUID '{wellKnownGuid}'",
                manifest.ServiceName,
                manifest.WellKnownTenantGuid);

            ITenant newTenant = await tenantStore.CreateWellKnownChildTenantAsync(
                parent.Id,
                manifest.WellKnownTenantGuid,
                manifest.ServiceName).ConfigureAwait(false);

            logger?.LogDebug(
                "New service tenant '{serviceName}' created with Id '{tenantId}'; updating tenant type and manifest.",
                newTenant.Name,
                newTenant.Id);

            IEnumerable <KeyValuePair <string, object> > properties = PropertyBagValues.Build(p => p
                                                                                              .AddServiceManifest(manifest)
                                                                                              .AddMarainTenantType(MarainTenantType.Service));

            await tenantStore.UpdateTenantAsync(
                newTenant.Id,
                propertiesToSetOrAdd : properties).ConfigureAwait(false);

            logger?.LogInformation(
                "Created new service tenant '{serviceName}' with Id '{tenantId}'.",
                newTenant.Name,
                newTenant.Id);
            return(newTenant);
        }
        /// <summary>
        /// Creates a new tenant representing a client, using a well known Guid.
        /// </summary>
        /// <param name="tenantStore">The tenant store.</param>
        /// <param name="wellKnownGuid">The well known Guid to use when creating the tenant.</param>
        /// <param name="clientName">The name of the new tenant.</param>
        /// <param name="parentId">Optional ID of parent Client tenant.</param>
        /// <param name="logger">Optional logger.</param>
        /// <returns>The new tenant.</returns>
        public static async Task <ITenant> CreateClientTenantWithWellKnownGuidAsync(this ITenantStore tenantStore, Guid wellKnownGuid, string clientName, string?parentId = null, ILogger?logger = null)
        {
            if (string.IsNullOrWhiteSpace(clientName))
            {
                throw new ArgumentException(nameof(clientName));
            }

            ITenant parent;

            if (!string.IsNullOrEmpty(parentId))
            {
                parent = await tenantStore.GetTenantOfTypeAsync(parentId, MarainTenantType.Client).ConfigureAwait(false);
            }
            else
            {
                parent = await tenantStore.GetClientTenantParentAsync().ConfigureAwait(false);
            }

            logger?.LogDebug("Creating new client tenant '{clientName}' with GUID '{wellKnownGuid}'", clientName, wellKnownGuid);
            ITenant newTenant = await tenantStore.CreateWellKnownChildTenantAsync(
                parent.Id,
                wellKnownGuid,
                clientName).ConfigureAwait(false);

            logger?.LogDebug(
                "New client tenant '{clientName}' created with Id '{tenantId}'; updating tenant type.",
                newTenant.Name,
                newTenant.Id);

            await tenantStore.UpdateTenantAsync(
                newTenant.Id,
                propertiesToSetOrAdd : PropertyBagValues.Build(p => p.AddMarainTenantType(MarainTenantType.Client))).ConfigureAwait(false);

            logger?.LogInformation(
                "Created new client tenant '{clientName}' with Id '{tenantId}'.",
                newTenant.Name,
                newTenant.Id);
            return(newTenant);
        }
        private static async Task <ITenant> CreateDelegatedTenant(this ITenantStore tenantStore, ITenant accessingTenant, ITenant serviceTenant, ILogger?logger = null)
        {
            string delegatedTenantName = TenantNames.DelegatedTenant(serviceTenant.Name, accessingTenant.Name);

            logger?.LogDebug("Creating new delegated tenant '{delegatedTenantName}'", delegatedTenantName);
            ITenant delegatedTenant = await tenantStore.CreateChildTenantAsync(serviceTenant.Id, delegatedTenantName).ConfigureAwait(false);

            logger?.LogDebug(
                "New delegated tenant '{delegatedTenantName}' created with Id '{tenantId}'; updating tenant type.",
                delegatedTenant.Name,
                delegatedTenant.Id);

            delegatedTenant = await tenantStore.UpdateTenantAsync(
                delegatedTenant.Id,
                propertiesToSetOrAdd : PropertyBagValues.Build(p => p.AddMarainTenantType(MarainTenantType.Delegated)))
                              .ConfigureAwait(false);

            logger?.LogInformation(
                "Created new delegated tenant '{delegatedTenantName}' with Id '{tenantId}'.",
                delegatedTenant.Name,
                delegatedTenant.Id);
            return(delegatedTenant);
        }