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

namespace ladaexpress.vn.Controllers
{
    public class JobPostController : Controller
    {
        private readonly IJobService _jobPostService;
        private readonly ISeoService _seoService;
        private readonly IStructuredDataService _structuredDataService;
        private readonly IBreadcrumbService _breadcrumbService;
        private readonly IContentOptimizationService _contentOptimizationService;

        public JobPostController(
            IJobService jobPostService,
            ISeoService seoService,
            IStructuredDataService structuredDataService,
            IBreadcrumbService breadcrumbService,
            IContentOptimizationService contentOptimizationService)
        {
            _jobPostService = jobPostService;
            _seoService = seoService;
            _structuredDataService = structuredDataService;
            _breadcrumbService = breadcrumbService;
            _contentOptimizationService = contentOptimizationService;
        }



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

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

            // Generate structured data for job listing page
            var structuredData = await _structuredDataService.GetCollectionPageStructuredDataAsync(
                categoryId: 0,
                name: "Tuyển dụng LADA Express",
                description: "Danh sách các vị trí tuyển dụng tại LADA Express",
                url: "/tuyen-dung",
                totalItems: model.JobPosts?.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 _jobPostService.PrepareJobDetailModel(id);
            if (model.Id > 0)
            {
                // Optimize content using ContentOptimizationService
                var keywords = await _contentOptimizationService.ExtractKeywordsAsync(
                    model.Title ?? "Tuyển dụng",
                    model.Description ?? "Cơ hội nghề nghiệp tại LADA Express"
                );
                var optimizedTitle = await _contentOptimizationService.GenerateOptimizedTitleAsync(
                    model.Title ?? "Tuyển dụng",
                    "Job",
                    keywords.FirstOrDefault()
                );
                var optimizedDescription = await _contentOptimizationService.GenerateOptimizedDescriptionAsync(
                    model.Description ?? "Cơ hội nghề nghiệp tại LADA Express",
                    "Job"
                );
                var readingTime = _contentOptimizationService.CalculateReadingTime(
                    model.Description ?? "Cơ hội nghề nghiệp tại LADA Express"
                );

                // Generate SEO data for job detail page
                var seoData = await _seoService.GetArticleSeoAsync(
                    postId: model.Id,
                    title: model.Title ?? "Tuyển dụng",
                    description: optimizedDescription ?? "Cơ hội nghề nghiệp tại LADA Express",
                    image: model.Thumbnail ?? "/images/default-job.jpg",
                    url: model.Slug ?? $"/tuyen-dung/{model.Id}",
                    author: "LADA Express HR",
                    publishedDate: model.StartDate?.DateTime ?? DateTime.Now,
                    modifiedDate: model.StartDate?.DateTime ?? DateTime.Now,
                    keywords: keywords
                );

                // Override with optimized content
                seoData.Title = optimizedTitle ?? model.Title ?? "Tuyển dụng";
                seoData.Description = optimizedDescription ?? "Cơ hội nghề nghiệp tại LADA Express";
                seoData.Keywords = string.Join(", ", keywords.Take(5));

                // Generate JobPosting structured data
                var jobStructuredData = await _structuredDataService.GetJobPostingStructuredDataAsync(
                    jobId: model.Id,
                    title: model.Title ?? "Tuyển dụng",
                    description: model.Description ?? "Cơ hội nghề nghiệp tại LADA Express",
                    workingAddress: model.WorkingAddress ?? "Hà Nội",
                    minSalary: model.MinSalary,
                    maxSalary: model.MaxSalary,
                    currency: "VND",
                    employmentType: "FULL_TIME",
                    datePosted: model.StartDate?.DateTime ?? DateTime.Now,
                    validThrough: model.EndDate?.DateTime ?? DateTime.Now.AddDays(30),
                    url: model.Slug ?? $"/tuyen-dung/{model.Id}"
                );

                // Generate breadcrumb structured data
                var breadcrumbData = await _structuredDataService.GetBreadcrumbStructuredDataAsync(
                    new List<BreadcrumbItem>
                    {
                        new() { Name = "Trang chủ", Url = "/" },
                        new() { Name = "Tuyển dụng", Url = "/tuyen-dung" },
                        new() { Name = model.Title ?? "Tuyển dụng", Url = model.Slug ?? $"/tuyen-dung/{model.Id}" }
                    }
                );

                // SEO Quality Analysis
                var seoQuality = await _contentOptimizationService.AnalyzeSeoQualityAsync(
                    title: optimizedTitle ?? model.Title ?? "Tuyển dụng",
                    description: optimizedDescription ?? "Cơ hội nghề nghiệp tại LADA Express",
                    content: model.Description ?? "Cơ hội nghề nghiệp tại LADA Express",
                    keywords: keywords
                );

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

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

    }
}
