using System; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; using xAiModels.Interfaces; using xAiModels.Interfaces.Dtos; using xAiModels.Interfaces.Enrichers; using xAiModels.Models.Dtos; using xCommons.Extensions; using xIdentityService.Interfaces; using xIdentityService.Extensions; using xModels.Dtos; using System.Linq; namespace xAiModels.Providers.Enrichers { public class XAiConversationEnricher : IXAiConversationEnricher { private readonly IXIdentityProvider identityProvider; private readonly IXAiProjectRepositoryService projectRepositoryService; private readonly IXAiConversationTitleResourceProvider conversationTitleResourceProvider; public XAiConversationEnricher( IXIdentityProvider identityProvider, IXAiProjectRepositoryService projectRepositoryService, IXAiConversationTitleResourceProvider conversationTitleResourceProvider ) { // this.identityProvider = identityProvider; this.projectRepositoryService = projectRepositoryService; this.conversationTitleResourceProvider = conversationTitleResourceProvider; } /// /// Converts a Signle Entity to Dto ... /// /// /// /// /// public async Task EnrichAsync( XAiConversationDto source, XAiConversationDto destination, CancellationToken cancellationToken = default ) { await EnrichAsync( source: source, fillMessages: false, destination: destination, cancellationToken: cancellationToken ); } /// /// Converts a Collection of Entities to Dtos ... /// /// /// /// /// public async Task EnrichRangeAsync( IEnumerable sources, IEnumerable destinations, CancellationToken cancellationToken = default ) { await EnrichRangeAsync( sources: sources, fillMessages: false, destinations: destinations, cancellationToken: cancellationToken ); } /// /// Converts a Signle Entity to Dto ... /// /// /// /// /// /// public async Task EnrichAsync( XAiConversationDto source, XAiConversationDto destination, bool fillMessages = false, CancellationToken cancellationToken = default ) { // // Run independent async operations concurrently var ownerTask = ResolveOwnerAsync(source.OwnerId, cancellationToken); var projectTask = ResolveProjectAsync(source.ProjectId, cancellationToken); var titleLocalesTask = ResolveTitleLocalesAsync(source.Title, cancellationToken); var messagesTask = ResolveMessagesAsync(source.Id, cancellationToken); // // wait until All Tasks Resolved ... Task[] tasks = [ ownerTask, projectTask, titleLocalesTask ]; if (fillMessages) { tasks = [.. tasks, messagesTask]; } await Task.WhenAll(tasks); // // Attached Resolved Info to Destination ... destination.Owner = ownerTask.Result; destination.TitleResource = titleLocalesTask.Result; var project = projectTask.Result; if (!project.IsNullOrDefault()) { destination.ProjectTitle = project.Title; destination.ProjectDescription = project.Description; destination.ProjectTitleResource = project.TitleResource; destination.ProjectDescriptionResource = project.DescriptionResource; } if (!messagesTask.Result.IsNull()) { destination.Messages = [.. messagesTask.Result]; } } /// /// Converts a Collection of Entities to Dtos ... /// /// /// /// /// /// public async Task EnrichRangeAsync( IEnumerable sources, IEnumerable destinations, bool fillMessages = false, CancellationToken cancellationToken = default ) { // // Make Tasks Ready ... var tasks = sources.Zip(destinations, (src, dst) => EnrichAsync(src, dst, cancellationToken) ); // await Task.WhenAll(tasks); } // #region Private ... /// /// Reading Owner User Info ... /// /// /// /// private async Task ResolveOwnerAsync( string ownerId, CancellationToken cancellationToken = default ) { // XPersonDto result = null; // // Validate ... if (ownerId.IsNullOrEmpty()) { return result; } // // Try to Retrieve User Info ... var device = identityProvider.GetDevice(); var userInfo = await identityProvider.GetUserInfo( device: device, userSelectByParam: ownerId ); result = userInfo.ToXPersonDto(); // return result; } /// /// Reading Title Locales ... /// /// /// /// private async Task ResolveTitleLocalesAsync( string title, CancellationToken cancellationToken = default ) { // XResourceDto result = null; if (title.IsNullOrEmpty()) { return result; } // result = await conversationTitleResourceProvider.GetResource( owner: null, identifier: title, cancellationToken: cancellationToken ); // return result; } /// /// Retrieve Project ... /// /// /// /// private async Task ResolveProjectAsync( Guid projectId, CancellationToken cancellationToken = default ) { // XAiProjectDto result = null; if (projectId.IsNull() || projectId.IsDefaultGuid()) { return result; } // result = await projectRepositoryService.GetAsync( id: projectId, includeBuilder: null, ignoreSoftDeleteds: true, cancellationToken: cancellationToken ); // return result; } /// /// Retrieve Specified Conversation Messages ... /// /// /// /// private async Task> ResolveMessagesAsync( Guid conversationId, CancellationToken cancellationToken = default ) { // var result = new List(); // if (conversationId.IsDefaultGuid()) { return result; } // return result; } #endregion } }