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

namespace EventsAppQuest.Web.Application
{
    /// <summary>
    /// Armazenamento em memória da watchlist (casos observados, favoritos, fixados). Mock; sem persistência real.
    /// </summary>
    public static class WatchlistStore
    {
        private static readonly Dictionary<string, EventCycleWatchItem> ByCycleId = new Dictionary<string, EventCycleWatchItem>();
        private static readonly object Lock = new object();

        public static void Add(string cycleId, string opportunityId, string title, string watchReason = null)
        {
            if (string.IsNullOrEmpty(cycleId)) return;
            lock (Lock)
            {
                var id = "Watch_" + cycleId;
                var now = DateTime.UtcNow;
                if (ByCycleId.TryGetValue(cycleId, out var existing))
                {
                    existing.Title = title ?? existing.Title;
                    existing.OpportunityId = opportunityId ?? existing.OpportunityId;
                    existing.WatchReason = watchReason ?? existing.WatchReason;
                    existing.LastTouchedAt = now;
                    return;
                }
                ByCycleId[cycleId] = new EventCycleWatchItem
                {
                    WatchItemId = id,
                    CycleId = cycleId,
                    OpportunityId = opportunityId,
                    Title = title ?? ("Ciclo " + cycleId),
                    IsFavorite = false,
                    IsPinned = false,
                    WatchReason = watchReason,
                    AddedAt = now,
                    LastTouchedAt = now,
                    Notes = null
                };
            }
        }

        public static void Remove(string cycleId)
        {
            if (string.IsNullOrEmpty(cycleId)) return;
            lock (Lock)
                ByCycleId.Remove(cycleId);
        }

        public static EventCycleWatchItem Get(string cycleId)
        {
            if (string.IsNullOrEmpty(cycleId)) return null;
            lock (Lock)
                return ByCycleId.TryGetValue(cycleId, out var item) ? item : null;
        }

        public static List<EventCycleWatchItem> GetAll()
        {
            lock (Lock)
                return new List<EventCycleWatchItem>(ByCycleId.Values);
        }

        public static bool IsWatched(string cycleId)
        {
            if (string.IsNullOrEmpty(cycleId)) return false;
            lock (Lock)
                return ByCycleId.ContainsKey(cycleId);
        }

        public static void SetFavorite(string cycleId, bool isFavorite)
        {
            lock (Lock)
            {
                if (ByCycleId.TryGetValue(cycleId, out var item))
                {
                    item.IsFavorite = isFavorite;
                    item.LastTouchedAt = DateTime.UtcNow;
                }
            }
        }

        public static void SetPinned(string cycleId, bool isPinned)
        {
            lock (Lock)
            {
                if (ByCycleId.TryGetValue(cycleId, out var item))
                {
                    item.IsPinned = isPinned;
                    item.LastTouchedAt = DateTime.UtcNow;
                }
            }
        }

        public static void UpdateNotes(string cycleId, string notes)
        {
            lock (Lock)
            {
                if (ByCycleId.TryGetValue(cycleId, out var item))
                {
                    item.Notes = notes;
                    item.LastTouchedAt = DateTime.UtcNow;
                }
            }
        }

        public static void UpdateWatchReason(string cycleId, string watchReason)
        {
            lock (Lock)
            {
                if (ByCycleId.TryGetValue(cycleId, out var item))
                {
                    item.WatchReason = watchReason;
                    item.LastTouchedAt = DateTime.UtcNow;
                }
            }
        }

        public static void UpdateTitle(string cycleId, string title)
        {
            lock (Lock)
            {
                if (ByCycleId.TryGetValue(cycleId, out var item) && !string.IsNullOrEmpty(title))
                {
                    item.Title = title;
                    item.LastTouchedAt = DateTime.UtcNow;
                }
            }
        }
    }
}
