using UnityEngine;
using UnityEngine.UI;
using System.Collections.Generic;
using System.Linq;
using EventsAppQuest.Web.Application;
using EventsAppQuest.Web.Core;
using EventsAppQuest.Web.Domain;

namespace EventsAppQuest.Web.Presentation
{
    /// <summary>
    /// Visão simples do pipeline comercial na Web (estágios com contagem / lista de oportunidades).
    /// Pipeline: New Lead → Contacted → Proposal Sent → Quest Session Done → Follow-up → Ready to Close → Won / Lost.
    /// </summary>
    public class OpportunityPipelineView : MonoBehaviour, IWebContentRoot
    {
        private static readonly OpportunityStage[] PipelineStages =
        {
            OpportunityStage.NewLead,
            OpportunityStage.Contacted,
            OpportunityStage.ProposalSent,
            OpportunityStage.QuestSessionDone,
            OpportunityStage.FollowUp,
            OpportunityStage.ReadyToClose,
            OpportunityStage.Won,
            OpportunityStage.Lost
        };

        private Transform _content;

        public void SetContentRoot(Transform contentRoot) => _content = contentRoot;

        private void Start()
        {
            Refresh();
            WebRuntimeContext.OnDataChanged += Refresh;
        }
        private void OnDestroy() => WebRuntimeContext.OnDataChanged -= Refresh;

        public void Refresh()
        {
            if (_content == null) return;
            ClearContent(_content);
            var service = WebRuntimeContext.DataService;
            if (service == null) return;
            var opportunities = service.GetOpportunities();
            var font = Resources.GetBuiltinResource<Font>("LegacyRuntime.ttf");

            foreach (var stage in PipelineStages)
            {
                var inStage = opportunities.Where(o => o.Stage == stage).ToList();
                var stageLabel = GetStageLabel(stage);
                var count = inStage.Count;
                var go = new GameObject("PipelineStage_" + stage);
                go.transform.SetParent(_content, false);
                var rt = go.AddComponent<RectTransform>();
                rt.sizeDelta = new Vector2(0, 26);
                var text = go.AddComponent<Text>();
                text.text = $"{stageLabel}: {count}";
                text.font = font;
                text.fontSize = 14;
                if (count > 0)
                {
                    text.text += " — " + string.Join(", ", inStage.Take(3).Select(o => o.Title));
                    if (inStage.Count > 3) text.text += " …";
                }
            }
        }

        private static void ClearContent(Transform content)
        {
            for (var i = content.childCount - 1; i >= 0; i--)
                Destroy(content.GetChild(i).gameObject);
        }

        private static string GetStageLabel(OpportunityStage stage)
        {
            switch (stage)
            {
                case OpportunityStage.NewLead: return "New Lead";
                case OpportunityStage.Contacted: return "Contacted";
                case OpportunityStage.ProposalSent: return "Proposal Sent";
                case OpportunityStage.QuestSessionDone: return "Quest Session Done";
                case OpportunityStage.FollowUp: return "Follow-up";
                case OpportunityStage.ReadyToClose: return "Ready to Close";
                case OpportunityStage.Won: return "Won";
                case OpportunityStage.Lost: return "Lost";
                default: return stage.ToString();
            }
        }
    }
}
