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

namespace ladaexpress.vn.Controllers
{
    public class CategoryController : Controller
    {
        private readonly ICategoryService _categoryService;
        private readonly ISeoService _seoService;
        private readonly IStructuredDataService _structuredDataService;

        public CategoryController(
            ICategoryService categoryService,
            ISeoService seoService,
            IStructuredDataService structuredDataService)
        {
            _categoryService = categoryService;
            _seoService = seoService;
            _structuredDataService = structuredDataService;
        }

        public async Task<IActionResult> Detail(int id)
        {
            var model = await _categoryService.PrepareCategoryDetail(id);
            if (model.Id > 0)
            {
                // Generate SEO data for category page
                var seoData = await _seoService.GetCategorySeoAsync(
                    categoryId: model.Id,
                    name: model.Name,
                    description: model.Description,
                    url: $"/danh-muc/{id}" // Can be improved with slug if available
                );

                // Generate structured data for collection page
                var structuredData = await _structuredDataService.GetCollectionPageStructuredDataAsync(
                    categoryId: model.Id,
                    name: model.Name,
                    description: model.Description,
                    url: $"/danh-muc/{id}",
                    totalItems: null // Can be added if available in model
                );

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

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

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