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

namespace EventsAppQuest.Web.Application
{
    /// <summary>
    /// Armazenamento em memória das anotações gerenciais por caso. Mock; sem persistência real.
    /// </summary>
    public static class ManagerNotesStore
    {
        private static readonly Dictionary<string, EventCycleManagerNote> ByNoteId = new Dictionary<string, EventCycleManagerNote>();
        private static readonly object Lock = new object();
        private static int _counter;

        public static string Add(string cycleId, string title, string summary, EventCycleManagerNoteCategory category, EventCycleManagerNoteImportance importance, EventCycleManagerAuthorRole authorRole, string notes = null)
        {
            if (string.IsNullOrEmpty(cycleId)) return null;
            lock (Lock)
            {
                _counter++;
                var noteId = "Note_" + cycleId + "_" + _counter + "_" + DateTime.UtcNow.Ticks;
                var now = DateTime.UtcNow;
                var note = new EventCycleManagerNote
                {
                    NoteId = noteId,
                    CycleId = cycleId,
                    Title = title ?? "Sem título",
                    Summary = summary,
                    NoteCategory = category,
                    Importance = importance,
                    AuthorRole = authorRole,
                    CreatedAt = now,
                    UpdatedAt = now,
                    IsPinned = false,
                    Notes = notes
                };
                ByNoteId[noteId] = note;
                return noteId;
            }
        }

        public static void Update(string noteId, string title, string summary, EventCycleManagerNoteCategory? category, EventCycleManagerNoteImportance? importance, string notes)
        {
            lock (Lock)
            {
                if (!ByNoteId.TryGetValue(noteId, out var note)) return;
                if (title != null) note.Title = title;
                if (summary != null) note.Summary = summary;
                if (category.HasValue) note.NoteCategory = category.Value;
                if (importance.HasValue) note.Importance = importance.Value;
                if (notes != null) note.Notes = notes;
                note.UpdatedAt = DateTime.UtcNow;
            }
        }

        public static void Remove(string noteId)
        {
            lock (Lock)
                ByNoteId.Remove(noteId);
        }

        public static void SetPinned(string noteId, bool isPinned)
        {
            lock (Lock)
            {
                if (ByNoteId.TryGetValue(noteId, out var note))
                {
                    note.IsPinned = isPinned;
                    note.UpdatedAt = DateTime.UtcNow;
                }
            }
        }

        public static EventCycleManagerNote Get(string noteId)
        {
            lock (Lock)
                return ByNoteId.TryGetValue(noteId, out var n) ? n : null;
        }

        public static List<EventCycleManagerNote> GetByCycleId(string cycleId)
        {
            lock (Lock)
                return ByNoteId.Values.Where(n => n.CycleId == cycleId).OrderByDescending(n => n.UpdatedAt ?? n.CreatedAt).ToList();
        }

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