using Cauldron.Interfaces;
using Cauldron.Messages;

namespace Cauldron.Services;

/// <summary>
/// HTTP-based service for retrieving conversation message history from Morgana backend.
/// Implements synchronous HTTP GET to ensure complete history is loaded before UI updates.
/// </summary>
/// <remarks>
/// <para><strong>Architecture Note:</strong></para>
/// <para>Unlike real-time message delivery (which uses SignalR), history retrieval uses HTTP
/// to ensure synchronous, predictable loading behavior. This allows the UI to keep the
/// magical loader active until the complete history is loaded and rendered.</para>
/// <para><strong>Error Handling:</strong></para>
/// <list type="bullet">
/// <item>404 Not Found → Returns null (conversation expired/deleted)</item>
/// <item>500 Server Error → Returns null (backend error)</item>
/// <item>Network Error → Returns null (connection failure)</item>
/// </list>
/// </remarks>
public class ConversationHistoryService : IConversationHistoryService
{
    private readonly HttpClient _httpClient;
    private readonly ILogger _logger;

    /// <summary>
    /// Initializes a new instance of the ConversationHistoryService.
    /// </summary>
    /// <param name="httpClient">HTTP client for API calls (injected by DI)</param>
    /// <param name="logger">Logger instance for diagnostics</param>
    public ConversationHistoryService(
        HttpClient httpClient,
        ILogger logger)
    {
        _httpClient = httpClient;
        _logger = logger;
    }

    /// <inheritdoc/>
    public async Task<ConversationHistoryResponse?> GetHistoryAsync(string conversationId)
    {
        try
        {
            _logger.LogInformation("Retrieving history for conversation {ConversationId}", conversationId);

            HttpResponseMessage response = await _httpClient.GetAsync(
                $"/api/morgana/conversation/{conversationId}/history");

            if (response.IsSuccessStatusCode)
            {
                ConversationHistoryResponse? history = await response.Content
                    .ReadFromJsonAsync<ConversationHistoryResponse>();

                _logger.LogInformation(
                    "Retrieved {MessagesLength} messages for conversation {ConversationId}", history?.Messages?.Length ?? 0, conversationId);

                return history;
            }

            if (response.StatusCode == System.Net.HttpStatusCode.NotFound)
            {
                _logger.LogWarning("Conversation {ConversationId} not found (404)", conversationId);
                return null;
            }

            string errorContent = await response.Content.ReadAsStringAsync();
            _logger.LogError(
                "Failed to retrieve history for {ConversationId}: {ResponseStatusCode} - {ErrorContent}", conversationId, response.StatusCode, errorContent);
            return null;
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "Exception retrieving history for conversation {ConversationId}", conversationId);
            return null;
        }
    }
}