using Microsoft.EntityFrameworkCore;
using OpenHarbor.CQRS.DynamicQuery.Abstractions;

namespace Codex.Dal.QueryProviders;

/// <summary>
/// Queryable provider for listing conversations with filtering, sorting, and pagination
/// </summary>
public class ListConversationsQueryableProvider(CodexDbContext dbContext)
    : IQueryableProviderOverride<ListConversationsQueryItem>
{
    public Task<IQueryable<ListConversationsQueryItem>> GetQueryableAsync(
        object query,
        CancellationToken cancellationToken = default)
    {
        var result = dbContext.Conversations
            .AsNoTracking()
            .Select(c => new ListConversationsQueryItem
            {
                Id = c.Id,
                Title = c.Title,
                Summary = c.Summary,
                IsActive = c.IsActive,
                CreatedAt = c.StartedAt,
                LastMessageAt = c.LastMessageAt,
                MessageCount = c.Messages.Count,
                ExecutionCount = c.Executions.Count
            });

        return Task.FromResult(result);
    }
}
