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

namespace EventsAppQuest.Web.Producer.Application
{
    /// <summary>
    /// Armazenamento em memória do estado de aceite final do hotel por pedido (mock).
    /// </summary>
    public static class HotelFinalAcceptanceStore
    {
        private static readonly Dictionary<string, HotelFinalAcceptanceState> States = new Dictionary<string, HotelFinalAcceptanceState>();
        private static readonly object Lock = new object();

        public static HotelFinalAcceptanceState GetOrCreate(string orderId)
        {
            if (string.IsNullOrEmpty(orderId)) return null;
            lock (Lock)
            {
                if (States.TryGetValue(orderId, out var existing))
                    return existing;
                var state = new HotelFinalAcceptanceState
                {
                    ProducerOrderId = orderId,
                    IsHotelAcceptanceOpen = true,
                    AcceptanceStatus = HotelFinalAcceptanceStatus.PendingReview,
                    AcceptedAt = null,
                    RejectedAt = null,
                    NeedsFinalAdjustment = false,
                    FinalAcceptanceSummary = null,
                    HotelAcceptanceNotes = null,
                    ClosedAt = null,
                    IsOrderCycleClosed = false
                };
                States[orderId] = state;
                return state;
            }
        }

        public static void SetState(HotelFinalAcceptanceState state)
        {
            if (state == null || string.IsNullOrEmpty(state.ProducerOrderId)) return;
            lock (Lock)
                States[state.ProducerOrderId] = state;
        }

        public static HotelFinalAcceptanceState GetState(string orderId)
        {
            if (string.IsNullOrEmpty(orderId)) return null;
            lock (Lock)
                return States.TryGetValue(orderId, out var s) ? s : null;
        }

        public static bool IsOrderCycleClosed(string orderId)
        {
            var s = GetState(orderId);
            return s != null && s.IsOrderCycleClosed;
        }
    }
}
