using System.Collections.Generic;
using EventsAppQuest.Web.Communication.Core;
using EventsAppQuest.Web.Communication.Domain;

namespace EventsAppQuest.Web.Communication.Application
{
    /// <summary>
    /// Controlador/helper para comunicação operacional ligada ao ProducerOrder (mock).
    /// Abre thread ao gerar/acessar pedido; adiciona mensagens; regista pedidos de esclarecimento e respostas.
    /// </summary>
    public static class ProducerOrderCommunicationController
    {
        /// <summary>Cria thread para o pedido se ainda não existir. Chamar após gerar ProducerOrder.</summary>
        public static CommunicationThread EnsureThreadForOrder(string orderId, string opportunityId = null, string proposalId = null, string sessionId = null)
        {
            return CommunicationStore.GetOrCreateThread(orderId, opportunityId, proposalId, sessionId);
        }

        /// <summary>Obtém ou cria a thread do pedido.</summary>
        public static CommunicationThread GetOrCreateThread(string orderId, string opportunityId = null, string proposalId = null, string sessionId = null)
        {
            return CommunicationStore.GetOrCreateThread(orderId, opportunityId, proposalId, sessionId);
        }

        public static CommunicationThread GetThreadForOrder(string orderId) => CommunicationStore.GetThreadByOrderId(orderId);

        public static IReadOnlyList<CommunicationMessage> GetMessagesForOrder(string orderId, int maxCount = 50)
            => CommunicationStore.GetMessagesForOrder(orderId, maxCount);

        /// <summary>Adiciona mensagem à thread do pedido (hotel ou produtor).</summary>
        public static CommunicationMessage AddMessage(string orderId, CommunicationParticipantRole senderRole, CommunicationMessageType messageType, string content)
        {
            var thread = CommunicationStore.GetOrCreateThread(orderId);
            if (thread == null) return null;
            return CommunicationStore.AddMessage(thread.Id, orderId, senderRole, messageType, content);
        }

        /// <summary>Regista pedido de esclarecimento (ex.: do produtor).</summary>
        public static CommunicationMessage AddClarificationRequest(string orderId, CommunicationParticipantRole fromRole, string content)
        {
            return AddMessage(orderId, fromRole, CommunicationMessageType.ClarificationRequest, content);
        }

        /// <summary>Regista resposta de esclarecimento (ex.: do hotel).</summary>
        public static CommunicationMessage AddClarificationResponse(string orderId, CommunicationParticipantRole fromRole, string content)
        {
            return AddMessage(orderId, fromRole, CommunicationMessageType.ClarificationResponse, content);
        }

        /// <summary>Regista observação de status.</summary>
        public static CommunicationMessage AddStatusUpdate(string orderId, CommunicationParticipantRole fromRole, string content)
        {
            return AddMessage(orderId, fromRole, CommunicationMessageType.StatusUpdate, content);
        }

        /// <summary>Mensagem geral (nota).</summary>
        public static CommunicationMessage AddGeneralNote(string orderId, CommunicationParticipantRole fromRole, string content)
        {
            return AddMessage(orderId, fromRole, CommunicationMessageType.GeneralNote, content);
        }
    }
}
