using System;
using System.Collections.Generic;
using EventsAppQuest.Web.Producer.Domain;

namespace EventsAppQuest.Web.Producer.Application
{
    /// <summary>
    /// Armazenamento em memória dos relatórios finais de execução por pedido (mock).
    /// </summary>
    public static class FinalReportStore
    {
        private static readonly Dictionary<string, ProducerExecutionFinalReport> ReportsByOrder = new Dictionary<string, ProducerExecutionFinalReport>();
        private static readonly object Lock = new object();

        public static void SetReport(ProducerExecutionFinalReport report)
        {
            if (report == null || string.IsNullOrEmpty(report.ProducerOrderId)) return;
            lock (Lock)
                ReportsByOrder[report.ProducerOrderId] = report;
        }

        public static ProducerExecutionFinalReport GetReport(string orderId)
        {
            if (string.IsNullOrEmpty(orderId)) return null;
            lock (Lock)
                return ReportsByOrder.TryGetValue(orderId, out var r) ? r : null;
        }

        public static bool HasReport(string orderId)
        {
            if (string.IsNullOrEmpty(orderId)) return false;
            lock (Lock)
                return ReportsByOrder.ContainsKey(orderId);
        }

        public static bool IsOperationallyClosed(string orderId)
        {
            var r = GetReport(orderId);
            return r != null && r.ClosedByProducerAt.HasValue;
        }

        public static void SetHotelAcknowledged(string orderId, DateTime? at = null)
        {
            if (string.IsNullOrEmpty(orderId)) return;
            lock (Lock)
            {
                if (ReportsByOrder.TryGetValue(orderId, out var r))
                    r.HotelAcknowledgedAt = at ?? DateTime.UtcNow;
            }
        }
    }
}
