using System.Collections.Generic;
using System.Linq;
using EventsAppQuest.Application.Services;
using EventsAppQuest.Core;
using EventsAppQuest.Domain.Enums;
using EventsAppQuest.Domain.Models;

namespace EventsAppQuest.Infrastructure.Services
{
    /// <summary>
    /// Gera hotspots visuais a partir do roteiro guiado, tema, pacote, momentos e itens.
    /// </summary>
    public class MockProposalVisualHotspotService : IProposalVisualHotspotService
    {
        public IReadOnlyList<ProposalVisualHotspot> GenerateHotspots(EventProject project, EventProjectProposal proposal)
        {
            var hotspots = new List<ProposalVisualHotspot>();
            if (proposal == null) return hotspots;

            var guidedTourService = ServiceRegistry.Instance.Get<IProposalGuidedTourService>();
            var themeService = ServiceRegistry.Instance.Get<IVisualThemeService>();
            var packageService = ServiceRegistry.Instance.Get<ICommercialPackageService>();

            var tourSteps = proposal.GuidedTourSteps ?? new List<ProposalGuidedTourStep>();
            if (tourSteps.Count == 0 && guidedTourService != null)
            {
                var generated = guidedTourService.GenerateGuidedTour(project, proposal);
                if (generated != null) { proposal.GuidedTourSteps = new List<ProposalGuidedTourStep>(generated); tourSteps = proposal.GuidedTourSteps; }
            }

            var priority = 1;
            foreach (var step in tourSteps.OrderBy(s => s.DisplayOrder))
            {
                var hotspotType = MapFocusToHotspotType(step.FocusType, step.RelatedZoneTypes);
                var isPremium = step.FocusType == ProposalGuidedTourFocusType.PremiumUpsell;
                var salesScore = step.IsKeyMoment ? 85 : (isPremium ? 80 : 65);
                var duration = Math.Max(1, step.SuggestedDurationMinutes);
                var h = new ProposalVisualHotspot
                {
                    Id = "hotspot-" + step.Id,
                    Title = step.Title ?? "Destaque",
                    Description = step.Description ?? "",
                    RelatedZoneTypes = step.RelatedZoneTypes != null ? new List<string>(step.RelatedZoneTypes) : new List<string>(),
                    RelatedShowroomItemIds = step.RelatedShowroomItemIds != null ? new List<string>(step.RelatedShowroomItemIds) : new List<string>(),
                    RelatedGuidedTourStepId = step.Id,
                    HotspotType = hotspotType,
                    HighlightPriority = priority++,
                    SalesValueScore = salesScore,
                    SuggestedFocusDurationMinutes = duration,
                    IsPremiumUpsell = isPremium,
                    Notes = string.IsNullOrEmpty(step.SalesMessage) ? null : step.SalesMessage
                };
                hotspots.Add(h);
            }

            if (themeService != null && !string.IsNullOrEmpty(proposal.ThemeId) && !hotspots.Any(x => x.HotspotType == ProposalVisualHotspotType.ThemeHighlight))
            {
                var theme = themeService.GetById(proposal.ThemeId);
                if (theme != null)
                {
                    hotspots.Insert(1, new ProposalVisualHotspot
                    {
                        Id = "hotspot-theme",
                        Title = "Tema: " + theme.Name,
                        Description = theme.Description ?? "",
                        HotspotType = ProposalVisualHotspotType.ThemeHighlight,
                        HighlightPriority = priority++,
                        SalesValueScore = 75,
                        SuggestedFocusDurationMinutes = 3,
                        IsPremiumUpsell = false
                    });
                }
            }

            if (packageService != null && !string.IsNullOrEmpty(proposal.SelectedCommercialPackageId))
            {
                var pkg = packageService.GetById(proposal.SelectedCommercialPackageId);
                if (pkg != null && !string.IsNullOrEmpty(pkg.UpsellNotes) && !hotspots.Any(x => x.IsPremiumUpsell))
                {
                    hotspots.Add(new ProposalVisualHotspot
                    {
                        Id = "hotspot-upsell",
                        Title = "Upgrade premium",
                        Description = pkg.UpsellNotes,
                        HotspotType = ProposalVisualHotspotType.PremiumDecor,
                        HighlightPriority = priority++,
                        SalesValueScore = 80,
                        SuggestedFocusDurationMinutes = 2,
                        IsPremiumUpsell = true
                    });
                }
            }

            return hotspots.OrderBy(x => x.HighlightPriority).ToList();
        }

        private static ProposalVisualHotspotType MapFocusToHotspotType(ProposalGuidedTourFocusType focus, List<string> zoneTypes)
        {
            switch (focus)
            {
                case ProposalGuidedTourFocusType.Welcome: return ProposalVisualHotspotType.EntranceImpact;
                case ProposalGuidedTourFocusType.ThemeHighlight: return ProposalVisualHotspotType.ThemeHighlight;
                case ProposalGuidedTourFocusType.MomentHighlight:
                    if (zoneTypes != null)
                    {
                        if (zoneTypes.Contains("DiningArea")) return ProposalVisualHotspotType.DiningExperience;
                        if (zoneTypes.Contains("StageArea")) return ProposalVisualHotspotType.StageMoment;
                        if (zoneTypes.Contains("LoungeArea")) return ProposalVisualHotspotType.LoungeExperience;
                    }
                    return ProposalVisualHotspotType.PremiumDecor;
                case ProposalGuidedTourFocusType.SupplierHighlight: return ProposalVisualHotspotType.SupplierShowcase;
                case ProposalGuidedTourFocusType.PremiumUpsell: return ProposalVisualHotspotType.PremiumDecor;
                case ProposalGuidedTourFocusType.OperationalConfidence: return ProposalVisualHotspotType.OperationalConfidence;
                case ProposalGuidedTourFocusType.Closing: return ProposalVisualHotspotType.ClosingVisual;
                case ProposalGuidedTourFocusType.SpaceOverview: return ProposalVisualHotspotType.EntranceImpact;
                default: return ProposalVisualHotspotType.ThemeHighlight;
            }
        }
    }
}
