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

namespace EventsAppQuest.Web.Application
{
    /// <summary>
    /// Controller da trilha de decisões executivas do caso. Agregador/manual; em memória.
    /// Não substitui módulos detalhados; sem dependência circular com snapshots/notas.
    /// </summary>
    public static class EventCycleExecutiveDecisionTrailController
    {
        public static EventCycleExecutiveDecisionTrailState GetTrailState(string cycleId)
        {
            var decisions = ExecutiveDecisionTrailStore.GetByCycle(cycleId ?? "");
            var latest = decisions.Count > 0 ? decisions[0] : null;
            var lastAt = latest?.CreatedAt;
            var summary = latest == null
                ? "Nenhuma decisão registada."
                : $"{latest.Title} | {latest.DecisionOutcome} | {latest.DecisionCategory}";

            return new EventCycleExecutiveDecisionTrailState
            {
                CycleId = cycleId,
                Decisions = decisions,
                LatestDecision = latest,
                TotalDecisions = decisions.Count,
                LastDecisionAt = lastAt,
                DecisionTrailSummary = summary
            };
        }

        public static string AddDecision(string cycleId, string title, string summary,
            Core.EventCycleExecutiveDecisionCategory category, Core.EventCycleExecutiveDecisionOutcome outcome,
            string reason, Core.EventCycleExecutiveDecisionRole role,
            string relatedSnapshotId = null, string relatedSectionKey = null, string notes = null)
        {
            return ExecutiveDecisionTrailStore.Add(cycleId, title, summary, category, outcome, reason, role,
                relatedSnapshotId, relatedSectionKey, notes);
        }

        public static void UpdateDecision(string decisionId, string title, string summary,
            Core.EventCycleExecutiveDecisionCategory? category, Core.EventCycleExecutiveDecisionOutcome? outcome,
            string reason, Core.EventCycleExecutiveDecisionRole? role,
            string relatedSnapshotId, string relatedSectionKey, string notes)
        {
            ExecutiveDecisionTrailStore.Update(decisionId, title, summary, category, outcome, reason, role,
                relatedSnapshotId, relatedSectionKey, notes);
        }

        public static void RemoveDecision(string decisionId)
        {
            ExecutiveDecisionTrailStore.Remove(decisionId);
        }

        public static EventCycleExecutiveDecision GetDecision(string decisionId)
        {
            return ExecutiveDecisionTrailStore.Get(decisionId);
        }

        public static List<EventCycleExecutiveDecision> ListDecisions(string cycleId)
        {
            return ExecutiveDecisionTrailStore.GetByCycle(cycleId ?? "");
        }

        public static bool HasTrail(string cycleId)
        {
            return ExecutiveDecisionTrailStore.HasTrail(cycleId ?? "");
        }

        public static int GetTotalDecisions(string cycleId)
        {
            return ExecutiveDecisionTrailStore.GetByCycle(cycleId ?? "").Count;
        }

        /// <summary>Estado agregado global (todas as decisões) para strip/dashboard.</summary>
        public static EventCycleExecutiveDecisionTrailState GetGlobalTrailState()
        {
            var all = ExecutiveDecisionTrailStore.GetAll();
            var latest = all.Count > 0 ? all[0] : null;
            var lastAt = latest?.CreatedAt;
            var summary = latest == null
                ? "Nenhuma decisão registada."
                : $"{latest.Title} [{latest.CycleId}] | {latest.DecisionOutcome} | {latest.DecisionCategory}";

            return new EventCycleExecutiveDecisionTrailState
            {
                CycleId = null,
                Decisions = all,
                LatestDecision = latest,
                TotalDecisions = all.Count,
                LastDecisionAt = lastAt,
                DecisionTrailSummary = summary
            };
        }
    }
}
