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

namespace EventsAppQuest.Web.Application
{
    /// <summary>
    /// Anotações gerenciais por caso e contexto executivo. Agregador manual; não substitui resumo executivo nem outros módulos.
    /// </summary>
    public static class EventCycleManagerNotesController
    {
        /// <summary>Adiciona uma nota gerencial ao caso.</summary>
        public static string AddNote(string cycleId, string title, string summary, EventCycleManagerNoteCategory category, EventCycleManagerNoteImportance importance, EventCycleManagerAuthorRole authorRole = EventCycleManagerAuthorRole.HotelAdmin, string notes = null)
        {
            return ManagerNotesStore.Add(cycleId, title, summary, category, importance, authorRole, notes);
        }

        /// <summary>Actualiza uma nota existente.</summary>
        public static void UpdateNote(string noteId, string title = null, string summary = null, EventCycleManagerNoteCategory? category = null, EventCycleManagerNoteImportance? importance = null, string notes = null)
        {
            ManagerNotesStore.Update(noteId, title, summary, category, importance, notes);
        }

        /// <summary>Remove uma nota.</summary>
        public static void RemoveNote(string noteId)
        {
            ManagerNotesStore.Remove(noteId);
        }

        /// <summary>Fixa ou desafixa uma nota.</summary>
        public static void SetNotePinned(string noteId, bool isPinned)
        {
            ManagerNotesStore.SetPinned(noteId, isPinned);
        }

        /// <summary>Obtém o estado das notas de um caso.</summary>
        public static EventCycleManagerNotesState GetNotesState(string cycleId, DateTime? updatedAt = null)
        {
            var now = updatedAt ?? DateTime.UtcNow;
            var list = ManagerNotesStore.GetByCycleId(cycleId ?? "");
            var pinned = list.Where(n => n.IsPinned).ToList();
            var topPinned = pinned.FirstOrDefault();
            var summary = list.Count == 0
                ? "Nenhuma anotação."
                : $"{list.Count} nota(s) | {(topPinned != null ? "Fixada: " + topPinned.Title : "")}";
            var lastUpdated = list.Count > 0 ? list.Max(n => n.UpdatedAt ?? n.CreatedAt) : (DateTime?)null;

            return new EventCycleManagerNotesState
            {
                CycleId = cycleId,
                Notes = list,
                PinnedNotes = pinned,
                TotalNotes = list.Count,
                LastUpdatedAt = lastUpdated ?? now,
                NotesSummary = summary,
                TopPinnedItem = topPinned
            };
        }

        /// <summary>Obtém estado agregado de todas as notas (para dashboard global).</summary>
        public static EventCycleManagerNotesState GetGlobalNotesState(DateTime? updatedAt = null)
        {
            var now = updatedAt ?? DateTime.UtcNow;
            var all = ManagerNotesStore.GetAll();
            var pinned = all.Where(n => n.IsPinned).OrderByDescending(n => n.UpdatedAt ?? n.CreatedAt).ToList();
            var topPinned = pinned.FirstOrDefault();
            var summary = all.Count == 0
                ? "Nenhuma anotação gerencial."
                : $"{all.Count} nota(s) em {all.Select(n => n.CycleId).Distinct().Count()} caso(s) | {(topPinned != null ? "Fixada: " + topPinned.Title + " [" + topPinned.CycleId + "]" : "")}";
            var lastUpdated = all.Count > 0 ? all.Max(n => n.UpdatedAt ?? n.CreatedAt) : (DateTime?)null;

            return new EventCycleManagerNotesState
            {
                CycleId = null,
                Notes = all.OrderByDescending(n => n.UpdatedAt ?? n.CreatedAt).ToList(),
                PinnedNotes = pinned,
                TotalNotes = all.Count,
                LastUpdatedAt = lastUpdated ?? now,
                NotesSummary = summary,
                TopPinnedItem = topPinned
            };
        }
    }
}
