using ladaexpress.vn.Infrastructures.Services;
using ladaexpress.vn.Infrastructures.Services.Seo;
using ladaexpress.vn.Models;
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;

namespace ladaexpress.vn.Controllers
{
    public class HomeController : Controller
    {
        private readonly IHomeService _homeService;
        private readonly ISeoService _seoService;
        private readonly IStructuredDataService _structuredDataService;

        public HomeController(IHomeService homeService, ISeoService seoService, IStructuredDataService structuredDataService)
        {
            _homeService = homeService;
            _seoService = seoService;
            _structuredDataService = structuredDataService;
        }

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

            // Set up SEO data for home page
            var seoData = await _seoService.GetHomePageSeoAsync();
            ViewData["SeoData"] = seoData;

            // Set up structured data
            var organizationSchema = _structuredDataService.GenerateOrganizationSchema();
            var websiteSchema = _structuredDataService.GenerateWebsiteSchema();
            var breadcrumbSchema = _structuredDataService.GenerateBreadcrumbSchema(seoData.Breadcrumbs);

            var combinedSchema = _structuredDataService.CombineSchemas(organizationSchema, websiteSchema, breadcrumbSchema);
            ViewData["StructuredData"] = combinedSchema;

            return View(model);
        }

        public IActionResult Privacy()
        {
            return View();
        }

        public IActionResult ContactUs()
        {
            return View();
        }

        public IActionResult Service()
        {
            return View();
        }

        public async Task<IActionResult> Search(string q, int page = 1)
        {
            // Basic search implementation - can be enhanced with actual search service
            var query = q?.Trim() ?? "";
            var hasResults = !string.IsNullOrEmpty(query) && query.Length >= 2;
            var totalResults = hasResults ? 0 : 0; // Should be replaced with actual search results count

            // Generate SEO data for search page
            var seoData = await _seoService.GetSearchSeoAsync(
                query: query,
                url: $"/tim-kiem?q={Uri.EscapeDataString(query)}&page={page}",
                hasResults: hasResults,
                totalResults: totalResults
            );

            // Pass data to view
            ViewBag.SeoData = seoData;
            ViewBag.Query = query;
            ViewBag.HasResults = hasResults;
            ViewBag.TotalResults = totalResults;
            ViewBag.Page = page;

            return View();
        }

        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }
    }
}