using System;
using EventsAppQuest.Core;
using EventsAppQuest.Web.Application;
using EventsAppQuest.Web.Core;

namespace EventsAppQuest.Web.Bridge
{
    /// <summary>
    /// Adaptador que converte dados da sessão Quest (Core) em QuestSessionSnapshot (Web).
    /// Único ponto que referencia tipos Quest; usado quando se pretende alimentar o CRM com resultado real da Quest.
    /// Não é chamado pela camada Quest; pode ser invocado por um fluxo de "Exportar para CRM" ou por testes.
    /// </summary>
    public static class QuestOutcomeToSnapshotAdapter
    {
        public static QuestSessionSnapshot BuildFromOutcome(
            QuestCommercialOutcome outcome,
            QuestSessionExportPayload payload = null,
            QuestSessionHandoffReport handoff = null,
            QuestSessionQualityAssessment quality = null)
        {
            if (outcome == null)
                return null;

            var snapshot = new QuestSessionSnapshot
            {
                SessionId = outcome.SessionId,
                RecommendedProposalId = outcome.RecommendedProposalId,
                SelectedProposalId = outcome.SelectedProposalId,
                FinalDecisionState = MapDecisionState(outcome.FinalDecisionState),
                SuggestedNextAction = outcome.SuggestedNextAction,
                FollowUpPriority = MapFollowUpPriority(outcome.FollowUpPriority),
                OutcomeSummary = outcome.OutcomeSummary,
                ConfidenceLevel = outcome.ConfidenceLevel,
                Timestamp = outcome.Timestamp
            };
            if (outcome.ClosingCTAChosen) snapshot.ConfirmedCTA = "PrimaryCTA";
            else if (outcome.SecondaryCTAChosen) snapshot.ConfirmedCTA = "SecondaryCTA";

            if (payload != null)
            {
                snapshot.ProjectId = payload.ProjectId;
                snapshot.ConfirmedCTA = snapshot.ConfirmedCTA ?? payload.ConfirmedCTA;
                snapshot.SuggestedNextAction = snapshot.SuggestedNextAction ?? payload.SuggestedNextAction;
                snapshot.ClosingSummary = payload.ClosingSummary;
                snapshot.HasExportPayload = true;
            }
            if (handoff != null)
            {
                snapshot.OutcomeSummary = snapshot.OutcomeSummary ?? handoff.OutcomeSummary;
                snapshot.ConfidenceLevel = snapshot.ConfidenceLevel ?? handoff.FinalConfidenceLabel;
                snapshot.HasHandoffReport = true;
            }
            if (quality != null)
            {
                snapshot.QualityLabel = quality.QualityLabel;
                snapshot.EngagementLabel = quality.EngagementContributionScore >= 70 ? "High" : quality.EngagementContributionScore >= 40 ? "Medium" : "Low";
                snapshot.HasQualityAssessment = true;
            }
            snapshot.HasShareableSummary = handoff != null;
            return snapshot;
        }

        private static WebSessionDecisionState MapDecisionState(QuestCommercialDecisionState state)
        {
            switch (state)
            {
                case QuestCommercialDecisionState.ExplorationOnly: return WebSessionDecisionState.ExplorationOnly;
                case QuestCommercialDecisionState.Interested: return WebSessionDecisionState.Interested;
                case QuestCommercialDecisionState.NeedsFollowUp: return WebSessionDecisionState.NeedsFollowUp;
                case QuestCommercialDecisionState.ReadyToAdvance: return WebSessionDecisionState.ReadyToAdvance;
                case QuestCommercialDecisionState.ReadyToClose: return WebSessionDecisionState.ReadyToClose;
                default: return WebSessionDecisionState.Unknown;
            }
        }

        private static FollowUpPriority MapFollowUpPriority(int priority)
        {
            if (priority >= 3) return FollowUpPriority.Urgent;
            if (priority == 2) return FollowUpPriority.High;
            if (priority == 1) return FollowUpPriority.Medium;
            return FollowUpPriority.Low;
        }
    }
}
