using ladaexpress.vn.Infrastructures.Services;
using ladaexpress.vn.Infrastructures.Services.Seo;
using Microsoft.AspNetCore.Mvc;

namespace ladaexpress.vn.Controllers
{
    public class PostController : Controller
    {
        private readonly IPostService _postService;
        private readonly ISeoService _seoService;
        private readonly IStructuredDataService _structuredDataService;
        private readonly IContentOptimizationService _contentOptimizationService;

        public PostController(
            IPostService postService,
            ISeoService seoService,
            IStructuredDataService structuredDataService,
            IContentOptimizationService contentOptimizationService)
        {
            _postService = postService;
            _seoService = seoService;
            _structuredDataService = structuredDataService;
            _contentOptimizationService = contentOptimizationService;
        }

        public async Task<IActionResult> Index()
        {
            var model = await _postService.PreparePostIndex();

            // Generate SEO data for news listing page
            var seoData = await _seoService.GetCategorySeoAsync(
                categoryId: 0, // News listing page doesn't have specific category
                name: "Tin tức",
                description: "Cập nhật tin tức mới nhất về dịch vụ vận chuyển, logistics và xu hướng ngành từ LADA Express.",
                url: "/tin-tuc"
            );

            // Generate structured data for news collection page
            var structuredData = await _structuredDataService.GetCollectionPageStructuredDataAsync(
                categoryId: 0,
                name: "Tin tức LADA Express",
                description: "Danh sách tin tức và bài viết mới nhất của LADA Express",
                url: "/tin-tuc",
                totalItems: model.RecentlyViewPosts?.Count
            );

            // Pass SEO data to view
            ViewBag.SeoData = seoData;
            ViewBag.StructuredData = structuredData;

            return View(model);
        }
        public async Task<IActionResult> Detail(int id)
        {
            var model = await _postService.PreparePostDetail(id);
            if (model.Id > 0)
            {
                // Parse dates for SEO
                DateTime publishedDate = DateTime.TryParse(model.PublishDate, out DateTime pubDate) ? pubDate : DateTime.Now;
                DateTime? modifiedDate = DateTime.TryParse(model.ModifiedDate, out DateTime modDate) ? modDate : null;

                // Optimize content using ContentOptimizationService
                var keywords = await _contentOptimizationService.ExtractKeywordsAsync(model.Title, model.Description);
                var optimizedTitle = await _contentOptimizationService.GenerateOptimizedTitleAsync(model.Title, "Post", keywords.FirstOrDefault());
                var optimizedDescription = await _contentOptimizationService.GenerateOptimizedDescriptionAsync(model.ShortDescription, "Post");
                var readingTime = _contentOptimizationService.CalculateReadingTime(model.Description);

                // Generate SEO data for article detail page
                var seoData = await _seoService.GetArticleSeoAsync(
                    postId: model.Id,
                    title: model.Title,
                    description: optimizedDescription, // Use optimized description
                    image: model.Thumbnail,
                    url: $"/{model.Slug}",
                    author: model.Author,
                    publishedDate: publishedDate,
                    modifiedDate: modifiedDate,
                    keywords: keywords
                );

                // Override with optimized content
                seoData.Title = optimizedTitle;
                seoData.Description = optimizedDescription;
                seoData.Keywords = string.Join(", ", keywords.Take(5));

                // Generate structured data for article
                var structuredData = await _structuredDataService.GetArticleStructuredDataAsync(
                    postId: model.Id,
                    title: model.Title,
                    description: optimizedDescription, // Use optimized description
                    image: model.Thumbnail,
                    url: $"/tin-tuc/{model.Slug}",
                    author: model.Author,
                    publishedDate: publishedDate,
                    modifiedDate: modifiedDate
                );

                // Generate breadcrumb structured data
                var breadcrumbData = await _structuredDataService.GetBreadcrumbStructuredDataAsync(
                    seoData.Breadcrumbs
                );

                // SEO Quality Analysis
                var seoQuality = await _contentOptimizationService.AnalyzeSeoQualityAsync(
                    title: optimizedTitle,
                    description: optimizedDescription,
                    content: model.Description,
                    keywords: keywords
                );

                // Pass SEO data to view
                ViewBag.SeoData = seoData;
                ViewBag.StructuredData = structuredData;
                ViewBag.BreadcrumbData = breadcrumbData;
                ViewBag.ReadingTime = readingTime;
                ViewBag.SeoQuality = seoQuality;
                ViewBag.ExtractedKeywords = keywords;

                return View(model);
            }
            else
            {
                return NotFound();
            }
        }
    }
}
