using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Codex.Dal;
using Codex.Dal.Enums;
using Microsoft.EntityFrameworkCore;
using OpenHarbor.CQRS.Abstractions;

namespace Codex.CQRS.Queries;

/// <summary>
/// Get conversation with all messages by ID
/// </summary>
public record GetConversationQuery
{
    /// <summary>Conversation ID</summary>
    public Guid Id { get; init; }
}

/// <summary>
/// Detailed conversation information with messages
/// </summary>
public record ConversationDetails
{
    /// <summary>Unique conversation identifier</summary>
    public Guid Id { get; init; }

    /// <summary>Conversation title</summary>
    public string Title { get; init; } = string.Empty;

    /// <summary>Conversation summary</summary>
    public string? Summary { get; init; }

    /// <summary>Whether conversation is active</summary>
    public bool IsActive { get; init; }

    /// <summary>Conversation start timestamp</summary>
    public DateTime StartedAt { get; init; }

    /// <summary>Last message timestamp</summary>
    public DateTime LastMessageAt { get; init; }

    /// <summary>Total message count</summary>
    public int MessageCount { get; init; }

    /// <summary>All messages in conversation</summary>
    public List<ConversationMessageItem> Messages { get; init; } = new();
}

/// <summary>
/// Individual message within a conversation
/// </summary>
public record ConversationMessageItem
{
    /// <summary>Message identifier</summary>
    public Guid Id { get; init; }

    /// <summary>Conversation identifier</summary>
    public Guid ConversationId { get; init; }

    /// <summary>Execution identifier if from agent execution</summary>
    public Guid? ExecutionId { get; init; }

    /// <summary>Message role (user, assistant, system, tool)</summary>
    public MessageRole Role { get; init; }

    /// <summary>Message content</summary>
    public string Content { get; init; } = string.Empty;

    /// <summary>Message index/order in conversation</summary>
    public int MessageIndex { get; init; }

    /// <summary>Whether message is in active context window</summary>
    public bool IsInActiveWindow { get; init; }

    /// <summary>Message creation timestamp</summary>
    public DateTime CreatedAt { get; init; }
}

public class GetConversationQueryHandler : IQueryHandler<GetConversationQuery, ConversationDetails?>
{
    private readonly CodexDbContext _dbContext;

    public GetConversationQueryHandler(CodexDbContext dbContext)
    {
        _dbContext = dbContext;
    }

    public async Task<ConversationDetails?> HandleAsync(
        GetConversationQuery query,
        CancellationToken cancellationToken = default)
    {
        return await _dbContext.Conversations
            .AsNoTracking()
            .Where(c => c.Id == query.Id)
            .Select(c => new ConversationDetails
            {
                Id = c.Id,
                Title = c.Title,
                Summary = c.Summary,
                IsActive = c.IsActive,
                StartedAt = c.StartedAt,
                LastMessageAt = c.LastMessageAt,
                MessageCount = c.MessageCount,
                Messages = c.Messages
                    .OrderBy(m => m.MessageIndex)
                    .Select(m => new ConversationMessageItem
                    {
                        Id = m.Id,
                        ConversationId = m.ConversationId,
                        ExecutionId = m.ExecutionId,
                        Role = m.Role,
                        Content = m.Content,
                        MessageIndex = m.MessageIndex,
                        IsInActiveWindow = m.IsInActiveWindow,
                        CreatedAt = m.CreatedAt
                    })
                    .ToList()
            })
            .FirstOrDefaultAsync(cancellationToken);
    }
}
