using Lada.Framework.Data.Enums;
using ladaexpress.vn.Infrastructures.Services;
using ladaexpress.vn.Infrastructures.Services.Seo;
using Microsoft.AspNetCore.Mvc;

namespace ladaexpress.vn.Controllers
{
    public class ProductController : Controller
    {
        private readonly IProductService _productService;
        private readonly ISeoService _seoService;
        private readonly IStructuredDataService _structuredDataService;
        private readonly IContentOptimizationService _contentOptimizationService;

        public ProductController(
            IProductService productService,
            ISeoService seoService,
            IStructuredDataService structuredDataService,
            IContentOptimizationService contentOptimizationService)
        {
            _productService = productService;
            _seoService = seoService;
            _structuredDataService = structuredDataService;
            _contentOptimizationService = contentOptimizationService;
        }


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

            // Generate SEO data for products listing page
            var seoData = await _seoService.GetProductIndexSeoAsync();

            // Generate structured data for collection page
            var structuredData = await _structuredDataService.GetCollectionPageStructuredDataAsync(
                categoryId: 0,
                name: "Dịch vụ LADA Express",
                description: "Danh sách các dịch vụ vận chuyển của LADA Express",
                url: "/dich-vu",
                totalItems: model.RecentlyViewProducts?.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 _productService.PrepareProductDetail(id);
            if (model.Id > 0)
            {
                // Optimize content using ContentOptimizationService
                var keywords = await _contentOptimizationService.ExtractKeywordsAsync(model.Name, model.Description);
                var optimizedTitle = await _contentOptimizationService.GenerateOptimizedTitleAsync(model.Name, "Product", keywords.FirstOrDefault());
                var optimizedDescription = await _contentOptimizationService.GenerateOptimizedDescriptionAsync(model.ShortDescription, "Product");
                var readingTime = _contentOptimizationService.CalculateReadingTime(model.Description);

                // Generate SEO data for product detail page
                var seoData = await _seoService.GetProductSeoAsync(
                    productId: model.Id,
                    name: model.Name,
                    description: optimizedDescription, // Use optimized description
                    image: model.Thumbnail,
                    url: $"{model.Slug}",
                    price: null // Price can be added if available in model
                );

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

                // Generate structured data for product
                var structuredData = await _structuredDataService.GetProductStructuredDataAsync(
                    productId: model.Id,
                    name: model.Name,
                    description: optimizedDescription, // Use optimized description
                    image: model.Thumbnail,
                    url: $"{model.Slug}",
                    price: null // Price can be added if available in model
                );

                // 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;

                switch (model.ProductType)
                {
                    case ProductType.General:
                        return View("Detail", model);
                    case ProductType.Bike:
                        return View("Detail-Bike", model);
                    // Add more cases as needed
                    default:
                        return View(model); // Default Detail.cshtml
                }
            }
            else
            {
                return NotFound();
            }
        }

    }
}
