using TakeoutSaaS.Domain.Tenants.Repositories;

namespace TakeoutSaaS.Module.Tenancy;

/// <summary>
/// 基于数据库的租户编码解析器实现。
/// </summary>
public sealed class DatabaseTenantCodeResolver(ITenantRepository tenantRepository) : ITenantCodeResolver
{
    /// <inheritdoc />
    public Task<long?> ResolveAsync(string code, CancellationToken cancellationToken = default)
    {
        // 1. 参数校验
        if (string.IsNullOrWhiteSpace(code))
        {
            return Task.FromResult<long?>(null);
        }

        // 2. 从数据库查询租户 ID
        return tenantRepository.FindIdByCodeAsync(code, cancellationToken);
    }
}
