using Lada.Framework.Data.Repositories;
using Lada.Framework.Infrastructures.Services;
using Lada.Framework.Infrastructures.AppSettings;
using Microsoft.Extensions.Options;
using ladaexpress.vn.Infrastructures.Routes;
using ladaexpress.vn.Infrastructures.Services;
using ladaexpress.vn.Infrastructures.Services.Seo;

namespace ladaexpress.vn.Infrastructures.StartupRegisters
{
    public static class DependencyRegister
    {
        public static void RegisterDependency(this IServiceCollection services, IConfiguration configuration)
        {
            #region AppSettings
            services.Configure<MediaProcessingSettings>(options => configuration.GetSection("Media").Bind(options));
            #endregion

            #region Repositories
            services.AddTransient(typeof(IRepository<>), typeof(Repository<>));
            #endregion


            services.AddScoped<RouteTransformer>();
            services.AddScoped<TagRouteTransformer>();
            services.AddScoped<ProductRouteTransformer>();
            services.AddScoped<JobPostRouteTransformer>();

            #region Services

            services.AddTransient<IPostService, PostService>();
            services.AddTransient<IProductService, ProductService>();
            services.AddTransient<ITopicService, TopicService>();
            services.AddTransient<ICategoryService, CategoryService>();
            services.AddTransient<ITagService, TagService>();
            services.AddTransient<IJobService, JobService>();
            services.AddTransient<IHomeService, HomeService>();

            services.AddTransient<ICvUploadService, CvUploadService>();
            services.AddTransient<ICandidateService, CandidateService>();
            services.AddTransient<IServiceService, ServiceService>();
            services.AddTransient<IUrlRecordService, UrlRecordService>();
            services.AddTransient<ILeadService, LeadService>();
            services.AddTransient<IScriptConfigService, ScriptConfigService>();

            // SEO Services
            services.AddTransient<ISeoService, SeoService>();
            services.AddTransient<IStructuredDataService, StructuredDataService>();
            services.AddTransient<IBreadcrumbService, BreadcrumbService>();
            services.AddTransient<IContentOptimizationService, ContentOptimizationService>();
            services.AddTransient<ISitemapService, SitemapService>();
            services.AddTransient<IRobotsService, RobotsService>();

            // AppConfig Services cho ladaexpress.vn
            services.AddScoped<ladaexpress.vn.Infrastructures.Services.IAppConfigService, ladaexpress.vn.Infrastructures.Services.AppConfigService>();

            // Sử dụng AppConfigService từ Framework (đã di chuyển) - cho backward compatibility
            services.AddScoped<Lada.Framework.Infrastructures.Services.IAppConfigService, Lada.Framework.Infrastructures.Services.AppConfigService>();

            // Media Services (chỉ cần IImageService để convert SystemPath → URL)
            services.AddScoped<IImageService>(provider =>
            {
                var settings = provider.GetRequiredService<IOptions<MediaProcessingSettings>>();
                var appConfigService = provider.GetService<Lada.Framework.Infrastructures.Services.IAppConfigService>();
                return new ImageService(settings, appConfigService);
            });

            #endregion

            // Register startup tasks
            services.AddStartupTasks();
        }
    }
}
