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

namespace EventsAppQuest.Web.Producer.Application
{
    /// <summary>
    /// Armazenamento em memória do checklist de preparação por pedido (mock).
    /// </summary>
    public static class PreparationChecklistStore
    {
        private static readonly List<ProducerPreparationChecklistItem> Items = new List<ProducerPreparationChecklistItem>();
        private static readonly Dictionary<string, PreparationConfirmedRecord> ConfirmedByOrder = new Dictionary<string, PreparationConfirmedRecord>();
        private static readonly object Lock = new object();

        private class PreparationConfirmedRecord
        {
            public DateTime ConfirmedAt;
            public string Summary;
        }

        public static void SetItems(string orderId, List<ProducerPreparationChecklistItem> items)
        {
            if (string.IsNullOrEmpty(orderId) || items == null) return;
            lock (Lock)
            {
                RemoveAllForOrder(orderId);
                foreach (var item in items)
                {
                    item.ProducerOrderId = orderId;
                    Items.Add(item);
                }
            }
        }

        public static void AddOrUpdateItem(ProducerPreparationChecklistItem item)
        {
            if (item == null) return;
            lock (Lock)
            {
                var idx = Items.FindIndex(i => i.ItemId == item.ItemId);
                if (idx >= 0)
                    Items[idx] = item;
                else
                    Items.Add(item);
            }
        }

        public static void RemoveAllForOrder(string orderId)
        {
            if (string.IsNullOrEmpty(orderId)) return;
            lock (Lock)
                Items.RemoveAll(i => i.ProducerOrderId == orderId);
        }

        public static IReadOnlyList<ProducerPreparationChecklistItem> GetItems(string orderId)
        {
            if (string.IsNullOrEmpty(orderId)) return new List<ProducerPreparationChecklistItem>();
            lock (Lock)
                return Items.Where(i => i.ProducerOrderId == orderId).ToList();
        }

        public static ProducerPreparationChecklistItem GetItem(string itemId)
        {
            if (string.IsNullOrEmpty(itemId)) return null;
            lock (Lock)
                return Items.FirstOrDefault(i => i.ItemId == itemId);
        }

        public static void SetPreparationConfirmed(string orderId, string summary)
        {
            if (string.IsNullOrEmpty(orderId)) return;
            lock (Lock)
                ConfirmedByOrder[orderId] = new PreparationConfirmedRecord { ConfirmedAt = DateTime.UtcNow, Summary = summary ?? "" };
        }

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

        public static DateTime? GetPreparationConfirmedAt(string orderId)
        {
            if (string.IsNullOrEmpty(orderId)) return null;
            lock (Lock)
                return ConfirmedByOrder.TryGetValue(orderId, out var r) ? r.ConfirmedAt : (DateTime?)null;
        }

        public static string GetPreparationSummary(string orderId)
        {
            if (string.IsNullOrEmpty(orderId)) return null;
            lock (Lock)
                return ConfirmedByOrder.TryGetValue(orderId, out var r) ? r.Summary : null;
        }
    }
}
