@using QRRapidoApp.Services
@using Microsoft.Extensions.Localization
@model dynamic
@inject AdDisplayService AdService
@inject IStringLocalizer<QRRapidoApp.Resources.SharedResource> Localizer
@{

    var userId = User?.FindFirst(System.Security.Claims.ClaimTypes.NameIdentifier)?.Value;
    var showAds = await AdService.ShouldShowAds(userId);
    var position = ViewBag.position ?? Model?.position ?? "header";
    var tagAdSense = ViewBag.AdSenseTag;
    <!-- AdSense -->
    @Html.Raw(ViewBag.AdSenseScript);
}

@if (showAds)
{
    @switch (position)
    {
        case "header":
            <div class="ad-container ad-header mb-4">
                <div class="ad-label">@Localizer["Advertisement"]</div>
                <ins class="adsbygoogle" 
                     style="display:inline-block;width:728px;height:90px"
                     data-ad-client="@tagAdSense"
                     data-ad-slot="QR19750801"></ins>
            </div>
            break;
            
        case "sidebar":
            <div class="ad-container ad-sidebar mb-4">
                <div class="ad-label">@Localizer["Advertisement"]</div>
                <ins class="adsbygoogle" 
                     style="display:inline-block;width:300px;height:250px"
                     data-ad-client="@tagAdSense"
                     data-ad-slot="QR19750802"></ins>
            </div>
            break;
            
        case "footer":
            <div class="ad-container ad-footer mt-5 mb-4">
                <div class="ad-label">@Localizer["Advertisement"]</div>
                <ins class="adsbygoogle" 
                     style="display:inline-block;width:728px;height:90px"
                     data-ad-client="@tagAdSense"
                     data-ad-slot="QR19750803"></ins>
            </div>
            break;
            
        case "content":
            <div class="ad-container ad-content my-4">
                <div class="ad-label">@Localizer["Advertisement"]</div>
                <ins class="adsbygoogle" 
                     style="display:block"
                     data-ad-client="@tagAdSense"
                     data-ad-slot="QR19750804"
                     data-ad-format="auto"
                     data-full-width-responsive="true"></ins>
            </div>
            break;
    }
    
    <script defer>
        // Lazy load AdSense to improve LCP
        if ('IntersectionObserver' in window) {
            const observer = new IntersectionObserver((entries) => {
                entries.forEach(entry => {
                    if (entry.isIntersecting) {
                        (adsbygoogle = window.adsbygoogle || []).push({});
                        observer.unobserve(entry.target);
                    }
                });
            });
            document.querySelectorAll('.adsbygoogle').forEach(ad => observer.observe(ad));
        } else {
            // Fallback for older browsers
            (adsbygoogle = window.adsbygoogle || []).push({});
        }
    </script>
}
else if (User.Identity.IsAuthenticated)
{
    var isPremium = await AdService.HasValidPremiumSubscription(userId);
    if (isPremium)
    {
        <!-- Premium User Message -->
        <div class="alert alert-success ad-free-notice mb-3">
            <i class="fas fa-crown text-warning"></i>
            <span><strong>@Localizer["PremiumUserNoAds"]</strong></span>
        </div>
    }
    else
    {
        <!-- Upgrade to Premium Message -->
        <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>
    }
}