@using QRRapidoApp.Services
@using QRRapidoApp.Services.Ads
@using QRRapidoApp.Models.Ads
@using Microsoft.Extensions.Localization
@using System.Globalization
@model dynamic
@inject AdDisplayService AdService
@inject IAdSlotConfigurationProvider SlotProvider
@inject IStringLocalizer<QRRapidoApp.Resources.SharedResource> Localizer
@{
    var userId = User?.FindFirst(System.Security.Claims.ClaimTypes.NameIdentifier)?.Value;
    var showAds = await AdService.ShouldShowAds(userId);
    var rawPosition = (ViewBag.position ?? Model?.position ?? "header")?.ToString()?.ToLowerInvariant();
    var position = string.IsNullOrWhiteSpace(rawPosition) ? "header" : rawPosition;
    var currentCulture = CultureInfo.CurrentUICulture?.Name;
    var slotConfig = SlotProvider.GetSlot(position, currentCulture);
    var adSenseClientId = ViewBag.AdSenseTag as string;
    var adSenseEnabled = ViewBag.AdSenseEnabled is bool enabled && enabled;
    var defaultAdSenseSlot = position switch
    {
        "header" => "QR19750801",
        "sidebar" => "QR19750802",
        "footer" => "QR19750803",
        "content" => "QR19750804",
        _ => "QR19750801"
    };
    var adSenseSlot = slotConfig.AdSenseSlotId ?? defaultAdSenseSlot;
    var isAffiliateProvider = string.Equals(slotConfig.Provider, "Affiliate", StringComparison.OrdinalIgnoreCase);
    var affiliateContent = isAffiliateProvider ? slotConfig.Affiliate : null;
    var containerCss = position switch
    {
        "header" => "ad-container ad-header mb-4",
        "sidebar" => "ad-container ad-sidebar mb-4",
        "footer" => "ad-container ad-footer mt-5 mb-4",
        "content" => "ad-container ad-content my-4",
        _ => "ad-container mb-4"
    };
    var shouldRenderAdSense = !isAffiliateProvider && adSenseEnabled && !string.IsNullOrWhiteSpace(adSenseClientId);
    var shouldRenderAffiliate = isAffiliateProvider && affiliateContent != null && !affiliateContent.IsEmpty();
    var renderedAdSense = false;
}

@if (showAds)
{
    switch (position)
    {
        case "header":
            if (shouldRenderAffiliate)
            {
                @await Html.PartialAsync("_AffiliateAd", new AffiliateAdViewModel
                {
                    SlotKey = position,
                    ContainerCssClass = containerCss,
                    Content = affiliateContent!
                })
            }
            else if (shouldRenderAdSense)
            {
                <!-- Placeholder for Adsterra ad - 728x90 Banner -->
                <div class="@containerCss" style="display: none;">
                    <!-- Adsterra code will go here when approved -->
                </div>
            }
            break;

        case "sidebar":
            if (shouldRenderAffiliate)
            {
                @await Html.PartialAsync("_AffiliateAd", new AffiliateAdViewModel
                {
                    SlotKey = position,
                    ContainerCssClass = containerCss,
                    Content = affiliateContent!
                })
            }
            else if (shouldRenderAdSense)
            {
                <!-- Placeholder for Adsterra ad - 300x250 Rectangle -->
                <div class="@containerCss" style="display: none;">
                    <!-- Adsterra code will go here when approved -->
                </div>
            }
            break;

        case "footer":
            if (shouldRenderAffiliate)
            {
                @await Html.PartialAsync("_AffiliateAd", new AffiliateAdViewModel
                {
                    SlotKey = position,
                    ContainerCssClass = containerCss,
                    Content = affiliateContent!
                })
            }
            else if (shouldRenderAdSense)
            {
                <!-- Placeholder for Adsterra ad - 728x90 Banner -->
                <div class="@containerCss" style="display: none;">
                    <!-- Adsterra code will go here when approved -->
                </div>
            }
            break;

        case "content":
            if (shouldRenderAffiliate)
            {
                @await Html.PartialAsync("_AffiliateAd", new AffiliateAdViewModel
                {
                    SlotKey = position,
                    ContainerCssClass = containerCss,
                    Content = affiliateContent!
                })
            }
            else if (shouldRenderAdSense)
            {
                <!-- Placeholder for Adsterra ad - Responsive -->
                <div class="@containerCss" style="display: none;">
                    <!-- Adsterra code will go here when approved -->
                </div>
            }
            break;

        default:
            if (shouldRenderAffiliate)
            {
                @await Html.PartialAsync("_AffiliateAd", new AffiliateAdViewModel
                {
                    SlotKey = position,
                    ContainerCssClass = containerCss,
                    Content = affiliateContent!
                })
            }
            else if (shouldRenderAdSense)
            {
                <!-- Placeholder for Adsterra ad - 728x90 Banner -->
                <div class="@containerCss" style="display: none;">
                    <!-- Adsterra code will go here when approved -->
                </div>
            }
            break;
    }

    @* AdSense lazy-load script removed - will be replaced with Adsterra when approved *@
}
else if (User.Identity.IsAuthenticated)
{
    var isPremium = await AdService.HasValidPremiumSubscription(userId);
    if (!isPremium)
    {
        @* Only show upgrade notice for non-premium users *@
        <div class="alert alert-info upgrade-notice mb-3">
            <i class="fas fa-star text-warning"></i>
            <span><strong>@Localizer["UpgradePremiumRemoveAds"]</strong></span>
            <a href="/Pagamento/SelecaoPlano" class="btn btn-sm btn-warning ms-2">
                <i class="fas fa-crown"></i> @Localizer["PremiumBenefitsShort"]
            </a>
        </div>
    }
    @* Premium users: render nothing (100% invisible) *@
}
