using Lada.Framework.Data.Domains.Cms;
using Lada.Framework.Data.Repositories;
using Microsoft.AspNetCore.Mvc.Routing;
using Microsoft.EntityFrameworkCore;

namespace ladaexpress.vn.Infrastructures.Routes
{
    public class JobPostRouteTransformer : DynamicRouteValueTransformer
    {
        //private readonly IBus _bus;
        private readonly IRepository<UrlRecord> _urlRecordRepository;
        public JobPostRouteTransformer(IRepository<UrlRecord> urlRecordRepository)
        {

            _urlRecordRepository = urlRecordRepository;
        }

        public override async ValueTask<RouteValueDictionary> TransformAsync(HttpContext httpContext, RouteValueDictionary values)
        {
            var controller = "JobPost";
            var action = "Index";
            var entityId = 0;


            if (!values.ContainsKey("slug"))
            {
                return values;
            }
            var requestPath = values["slug"].ToString();
            if (string.IsNullOrEmpty(requestPath)) return values;
            //var urlRecord = await _bus.Rpc.RequestAsync<string, UrlRecordDetail>(requestPath);
            var urlRecord = await _urlRecordRepository.Query().FirstOrDefaultAsync(u => u.Slug == $"tuyen-dung/{requestPath}");

            //var urlRecord = _urlRecordService.GetUrlRecordBySlug(requestPath);
            if (urlRecord != null && urlRecord.Id > 0)
            {
                action = "Detail";
                entityId = urlRecord.JobPostId.HasValue ? urlRecord.JobPostId.Value : 0;
            }
            else
            {
                controller = "Status";
                action = "NotFound";
            }

            values["controller"] = controller;
            values["action"] = action;
            values["id"] = entityId;
            return values;
        }
    }
}
