using EventsAppQuest.Core;
using EventsAppQuest.Domain.Enums;
using EventsAppQuest.Domain.Models;

namespace EventsAppQuest.Presentation
{
    /// <summary>
    /// Gerencia o modo de fechamento imersivo: abrir/fechar e preencher a partir de
    /// ProposalDecisionSummary e ProposalRecommendationSummary (recomendação e decisão da proposta).
    /// </summary>
    public class QuestClosingExperienceController : UnityEngine.MonoBehaviour
    {
        private QuestPresentationSession _session;

        /// <summary>Estado do fechamento actual (pode ser null).</summary>
        public QuestClosingExperienceState Current => _session?.CurrentClosingState;

        /// <summary>Liga o controlador à sessão.</summary>
        public void Bind(QuestPresentationSession session)
        {
            _session = session;
        }

        /// <summary>Abre o painel de fechamento imersivo, preenchendo a partir da recomendação e decisão.</summary>
        public void OpenClosing()
        {
            if (_session == null) return;
            var decision = _session.DecisionSummary;
            var rec = _session.RecommendationSummary;
            var headline = decision?.ExecutiveHeadline;
            var summary = decision?.ExecutiveShortSummary ?? rec?.ExecutiveSummary;
            var nextAction = decision?.SuggestedNextAction;
            var budget = decision?.BudgetConclusion ?? rec?.BudgetPositioning;
            var experience = decision?.ExperienceConclusion ?? rec?.ExperiencePositioning;
            var operational = decision?.OperationalConclusion ?? rec?.OperationalPositioning;
            var confidence = rec != null && rec.RecommendationConfidenceScore > 0
                ? "Confiança " + rec.RecommendationConfidenceScore + "%"
                : (decision != null ? "Decisão suportada" : "");
            var readyToClose = decision != null && decision.DecisionReadinessLevel == ProposalDecisionReadinessLevel.ReadyToClose;
            _session.ClosingOpenCount++;
            _session.CurrentClosingState = new QuestClosingExperienceState
            {
                IsClosingOpen = true,
                RecommendedProposalId = decision?.RecommendedProposalId ?? rec?.RecommendedProposalId ?? _session.Proposal?.Id,
                ClosingHeadline = !string.IsNullOrEmpty(headline) ? headline.Trim() : "Conclusão da proposta",
                ClosingSummary = !string.IsNullOrEmpty(summary) ? summary.Trim() : "Revise os pontos-chave e conduza ao fechamento.",
                PrimaryCTA = readyToClose ? "Fechar proposta" : "Aceitar conclusão",
                SecondaryCTA = "Ver alternativas",
                SuggestedNextAction = !string.IsNullOrEmpty(nextAction) ? nextAction.Trim() : "Apresentar proposta ao cliente",
                ConfidenceLabel = confidence,
                BudgetConclusion = !string.IsNullOrEmpty(budget) ? budget.Trim() : null,
                ExperienceConclusion = !string.IsNullOrEmpty(experience) ? experience.Trim() : null,
                OperationalConclusion = !string.IsNullOrEmpty(operational) ? operational.Trim() : null,
                IsReadyToClose = readyToClose
            };
            _session.LastClosingAction = null;
        }

        /// <summary>Fecha o painel de fechamento (volta ao tour/contexto/comparação).</summary>
        public void CloseClosing()
        {
            if (_session?.CurrentClosingState == null) return;
            _session.CurrentClosingState.IsClosingOpen = false;
        }

        /// <summary>Confirma o CTA principal (ex.: fechar proposta). Actualiza LastClosingAction para debug.</summary>
        public void ConfirmPrimaryCTA()
        {
            if (_session == null) return;
            _session.LastClosingAction = "PrimaryCTA";
            CloseClosing();
        }

        /// <summary>Confirma o CTA secundário (ex.: ver alternativas). Actualiza LastClosingAction para debug.</summary>
        public void ConfirmSecondaryCTA()
        {
            if (_session == null) return;
            _session.LastClosingAction = "SecondaryCTA";
            CloseClosing();
        }
    }
}
