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

namespace EventsAppQuest.Web.Producer.Application
{
    /// <summary>
    /// Armazenamento em memória dos pedidos gerados pelo hotel (handoff formal).
    /// Partilhado entre a web do hotel e a web do produtor; sem persistência real.
    /// </summary>
    public static class HandoffStore
    {
        private static readonly List<ProducerOrder> Orders = new List<ProducerOrder>();
        private static readonly object Lock = new object();

        public static void AddOrder(ProducerOrder order)
        {
            if (order == null) return;
            lock (Lock)
            {
                Orders.Add(order);
            }
        }

        public static IReadOnlyList<ProducerOrder> GetOrders()
        {
            lock (Lock)
            {
                return Orders.ToList();
            }
        }

        public static ProducerOrder GetOrderById(string orderId)
        {
            lock (Lock)
            {
                return Orders.FirstOrDefault(o => o.OrderId == orderId);
            }
        }

        public static void UpdateAcceptance(string orderId, ProducerAcceptanceStatus status, string note, bool requiresClarification = false, string clarificationReason = null)
        {
            lock (Lock)
            {
                var order = Orders.FirstOrDefault(o => o.OrderId == orderId);
                if (order == null) return;
                order.AcceptanceStatus = status;
                order.RequiresClarification = requiresClarification;
                order.ClarificationReason = clarificationReason ?? order.ClarificationReason;
                order.LastUpdatedAt = DateTime.UtcNow;
                if (status == ProducerAcceptanceStatus.Accepted)
                    order.AcceptedAt = DateTime.UtcNow;
                if (status == ProducerAcceptanceStatus.Rejected)
                    order.RejectedAt = DateTime.UtcNow;
                if (status == ProducerAcceptanceStatus.NeedsClarification)
                    order.RequiresClarification = true;
            }
        }
    }
}
