using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Localization; using QRRapidoApp.Services; using System.Security.Claims; namespace QRRapidoApp.Controllers { public class TutoriaisController : Controller { private readonly IMarkdownService _markdownService; private readonly AdDisplayService _adDisplayService; private readonly ILogger _logger; private readonly IStringLocalizer _localizer; private readonly IConfiguration _config; public TutoriaisController( IMarkdownService markdownService, AdDisplayService adDisplayService, ILogger logger, IStringLocalizer localizer, IConfiguration config) { _markdownService = markdownService; _adDisplayService = adDisplayService; _logger = logger; _localizer = localizer; _config = config; } [Route("{culture:regex(^(pt-BR|es-PY)$)}/tutoriais/{slug}")] public async Task Article(string slug, string culture) { try { var article = await _markdownService.GetArticleAsync(slug, culture); if (article == null) { _logger.LogWarning("Article not found: {Slug} ({Culture})", slug, culture); return NotFound(); } // Set ViewBag for ads and user info var userId = User?.FindFirst(ClaimTypes.NameIdentifier)?.Value; ViewBag.ShowAds = await _adDisplayService.ShouldShowAds(userId); ViewBag.IsPremium = await _adDisplayService.HasValidPremiumSubscription(userId ?? ""); ViewBag.IsAuthenticated = User.Identity?.IsAuthenticated ?? false; ViewBag.UserName = User.Identity?.Name ?? ""; _adDisplayService.SetViewBagAds(ViewBag); // Set SEO metadata from article ViewBag.Title = article.Metadata.Title; ViewBag.Description = article.Metadata.Description; ViewBag.Keywords = article.Metadata.Keywords; ViewBag.OgImage = article.Metadata.Image; ViewBag.OgType = "article"; ViewBag.ArticleAuthor = article.Metadata.Author; ViewBag.ArticlePublishedTime = article.Metadata.Date.ToString("yyyy-MM-ddTHH:mm:ssZ"); ViewBag.ArticleModifiedTime = article.Metadata.LastMod.ToString("yyyy-MM-ddTHH:mm:ssZ"); ViewBag.Culture = culture; ViewBag.Slug = slug; // Get related articles (same culture, exclude current) var allArticles = await _markdownService.GetAllArticlesAsync(culture); article.RelatedArticles = allArticles .Where(a => a.Title != article.Metadata.Title) .Take(3) .ToList(); _logger.LogInformation("Serving article: {Slug} ({Culture})", slug, culture); return View(article); } catch (Exception ex) { _logger.LogError(ex, "Error serving article: {Slug} ({Culture})", slug, culture); return StatusCode(500, "Internal server error"); } } [Route("{culture:regex(^(pt-BR|es-PY)$)}/tutoriais")] public async Task Index(string culture) { try { var articles = await _markdownService.GetAllArticlesAsync(culture); // Set ViewBag var userId = User?.FindFirst(ClaimTypes.NameIdentifier)?.Value; ViewBag.ShowAds = await _adDisplayService.ShouldShowAds(userId); ViewBag.IsPremium = await _adDisplayService.HasValidPremiumSubscription(userId ?? ""); ViewBag.IsAuthenticated = User.Identity?.IsAuthenticated ?? false; ViewBag.UserName = User.Identity?.Name ?? ""; _adDisplayService.SetViewBagAds(ViewBag); ViewBag.Title = culture == "pt-BR" ? "Tutoriais QR Code" : "Tutoriales Código QR"; ViewBag.Description = culture == "pt-BR" ? "Aprenda a criar e usar QR Codes com nossos tutoriais completos" : "Aprende a crear y usar códigos QR con nuestros tutoriales completos"; ViewBag.Culture = culture; _logger.LogInformation("Serving tutorials index ({Culture}): {Count} articles", culture, articles.Count); return View(articles); } catch (Exception ex) { _logger.LogError(ex, "Error serving tutorials index ({Culture})", culture); return StatusCode(500, "Internal server error"); } } } }