using Autofac;
using HoneyBox.Core.Interfaces;
using HoneyBox.Core.Services;
using HoneyBox.Model.Data;
using HoneyBox.Model.Models.Auth;
using HoneyBox.Model.Models.Payment;
using Microsoft.Extensions.Logging;

namespace HoneyBox.Infrastructure.Modules;

/// <summary>
/// 服务注册模块 - 用于注册业务服务
/// </summary>
public class ServiceModule : Module
{
    protected override void Load(ContainerBuilder builder)
    {
        // 注册 JWT 服务
        builder.RegisterType<JwtService>().As<IJwtService>().InstancePerLifetimeScope();

        // 注册用户服务
        builder.RegisterType<UserService>().As<IUserService>().InstancePerLifetimeScope();

        // 注册微信服务
        builder.Register(c =>
        {
            var httpClientFactory = c.Resolve<System.Net.Http.IHttpClientFactory>();
            var logger = c.Resolve<ILogger<WechatService>>();
            var wechatPaySettings = c.Resolve<Microsoft.Extensions.Options.IOptions<WechatPaySettings>>();
            var redisService = c.Resolve<IRedisService>();
            var dbContext = c.Resolve<HoneyBoxDbContext>();
            var configService = c.ResolveOptional<IWechatPayConfigService>();
            return new WechatService(httpClientFactory.CreateClient(), logger, wechatPaySettings, redisService, dbContext, configService);
        }).As<IWechatService>().InstancePerLifetimeScope();

        // 注册 IP 地理位置服务
        builder.Register(c =>
        {
            var httpClientFactory = c.Resolve<System.Net.Http.IHttpClientFactory>();
            var logger = c.Resolve<ILogger<IpLocationService>>();
            var amapSettings = c.Resolve<AmapSettings>();
            return new IpLocationService(httpClientFactory.CreateClient(), logger, amapSettings);
        }).As<IIpLocationService>().InstancePerLifetimeScope();

        // 注册认证服务
        builder.Register(c =>
        {
            var dbContext = c.Resolve<HoneyBoxDbContext>();
            var userService = c.Resolve<IUserService>();
            var jwtService = c.Resolve<IJwtService>();
            var wechatService = c.Resolve<IWechatService>();
            var ipLocationService = c.Resolve<IIpLocationService>();
            var redisService = c.Resolve<IRedisService>();
            var jwtSettings = c.Resolve<JwtSettings>();
            var logger = c.Resolve<ILogger<AuthService>>();
            return new AuthService(dbContext, userService, jwtService, wechatService, ipLocationService, redisService, jwtSettings, logger);
        }).As<IAuthService>().InstancePerLifetimeScope();

        // ========== 用户管理系统服务注册 ==========

        // 注册地址服务
        builder.Register(c =>
        {
            var dbContext = c.Resolve<HoneyBoxDbContext>();
            var logger = c.Resolve<ILogger<AddressService>>();
            return new AddressService(dbContext, logger);
        }).As<IAddressService>().InstancePerLifetimeScope();

        // 注册资产服务
        builder.Register(c =>
        {
            var dbContext = c.Resolve<HoneyBoxDbContext>();
            var logger = c.Resolve<ILogger<AssetService>>();
            return new AssetService(dbContext, logger);
        }).As<IAssetService>().InstancePerLifetimeScope();

        // 注册权益服务
        builder.Register(c =>
        {
            var dbContext = c.Resolve<HoneyBoxDbContext>();
            var logger = c.Resolve<ILogger<EquityService>>();
            return new EquityService(dbContext, logger);
        }).As<IEquityService>().InstancePerLifetimeScope();

        // 注册优惠券服务
        builder.Register(c =>
        {
            var dbContext = c.Resolve<HoneyBoxDbContext>();
            var logger = c.Resolve<ILogger<CouponService>>();
            return new CouponService(dbContext, logger);
        }).As<ICouponService>().InstancePerLifetimeScope();

        // 注册任务服务
        builder.Register(c =>
        {
            var dbContext = c.Resolve<HoneyBoxDbContext>();
            var logger = c.Resolve<ILogger<TaskService>>();
            return new TaskService(dbContext, logger);
        }).As<ITaskService>().InstancePerLifetimeScope();

        // 注册推荐服务
        builder.Register(c =>
        {
            var dbContext = c.Resolve<HoneyBoxDbContext>();
            var posterService = c.Resolve<IPosterService>();
            var logger = c.Resolve<ILogger<InvitationService>>();
            return new InvitationService(dbContext, posterService, logger);
        }).As<IInvitationService>().InstancePerLifetimeScope();

        // 注册排行榜服务
        builder.Register(c =>
        {
            var dbContext = c.Resolve<HoneyBoxDbContext>();
            var logger = c.Resolve<ILogger<RankService>>();
            return new RankService(dbContext, logger);
        }).As<IRankService>().InstancePerLifetimeScope();

        // 注册兑换码服务
        builder.Register(c =>
        {
            var dbContext = c.Resolve<HoneyBoxDbContext>();
            var logger = c.Resolve<ILogger<RedeemService>>();
            return new RedeemService(dbContext, logger);
        }).As<IRedeemService>().InstancePerLifetimeScope();

        // 注册福利屋服务
        builder.Register(c =>
        {
            var dbContext = c.Resolve<HoneyBoxDbContext>();
            var logger = c.Resolve<ILogger<WelfareService>>();
            return new WelfareService(dbContext, logger);
        }).As<IWelfareService>().InstancePerLifetimeScope();

        // ========== 商品系统服务注册 ==========

        // 注册商品缓存服务
        builder.Register(c =>
        {
            var redisService = c.Resolve<IRedisService>();
            var logger = c.Resolve<ILogger<GoodsCacheService>>();
            return new GoodsCacheService(redisService, logger);
        }).As<IGoodsCacheService>().InstancePerLifetimeScope();

        // 注册商品服务
        builder.Register(c =>
        {
            var dbContext = c.Resolve<HoneyBoxDbContext>();
            var cacheService = c.Resolve<IGoodsCacheService>();
            var logger = c.Resolve<ILogger<GoodsService>>();
            return new GoodsService(dbContext, cacheService, logger);
        }).As<IGoodsService>().InstancePerLifetimeScope();

        // 注册收藏服务
        builder.Register(c =>
        {
            var dbContext = c.Resolve<HoneyBoxDbContext>();
            var logger = c.Resolve<ILogger<CollectionService>>();
            var redisService = c.Resolve<IRedisService>();
            return new CollectionService(dbContext, logger, redisService);
        }).As<ICollectionService>().InstancePerLifetimeScope();

        // 注册奖品服务
        builder.Register(c =>
        {
            var dbContext = c.Resolve<HoneyBoxDbContext>();
            var logger = c.Resolve<ILogger<PrizeService>>();
            return new PrizeService(dbContext, logger);
        }).As<IPrizeService>().InstancePerLifetimeScope();

        // ========== 抽奖系统服务注册 ==========

        // 注册库存管理服务
        builder.Register(c =>
        {
            var dbContext = c.Resolve<HoneyBoxDbContext>();
            var logger = c.Resolve<ILogger<InventoryManager>>();
            return new InventoryManager(dbContext, logger);
        }).As<IInventoryManager>().InstancePerLifetimeScope();

        // 注册奖励发放服务
        builder.Register(c =>
        {
            var dbContext = c.Resolve<HoneyBoxDbContext>();
            var logger = c.Resolve<ILogger<RewardService>>();
            return new RewardService(dbContext, logger);
        }).As<IRewardService>().InstancePerLifetimeScope();

        // 注册抽奖引擎服务
        builder.Register(c =>
        {
            var dbContext = c.Resolve<HoneyBoxDbContext>();
            var inventoryManager = c.Resolve<IInventoryManager>();
            var rewardService = c.Resolve<IRewardService>();
            var logger = c.Resolve<ILogger<LotteryEngine>>();
            return new LotteryEngine(dbContext, inventoryManager, rewardService, logger);
        }).As<ILotteryEngine>().InstancePerLifetimeScope();

        // 注册抽奖服务
        builder.Register(c =>
        {
            var dbContext = c.Resolve<HoneyBoxDbContext>();
            var lotteryEngine = c.Resolve<ILotteryEngine>();
            var logger = c.Resolve<ILogger<LotteryService>>();
            return new LotteryService(dbContext, lotteryEngine, logger);
        }).As<ILotteryService>().InstancePerLifetimeScope();

        // ========== 订单系统服务注册 ==========

        // 注册物流服务
        builder.Register(c =>
        {
            var dbContext = c.Resolve<HoneyBoxDbContext>();
            var httpClientFactory = c.Resolve<System.Net.Http.IHttpClientFactory>();
            var logger = c.Resolve<ILogger<LogisticsService>>();
            return new LogisticsService(dbContext, httpClientFactory.CreateClient(), logger);
        }).As<ILogisticsService>().InstancePerLifetimeScope();

        // 注册订单服务
        builder.Register(c =>
        {
            var dbContext = c.Resolve<HoneyBoxDbContext>();
            var logger = c.Resolve<ILogger<OrderService>>();
            var lotteryEngine = c.Resolve<ILotteryEngine>();
            var wechatPayService = c.Resolve<IWechatPayService>();
            return new OrderService(dbContext, logger, lotteryEngine, wechatPayService);
        }).As<IOrderService>().InstancePerLifetimeScope();

        // 注册仓库服务
        builder.Register(c =>
        {
            var dbContext = c.Resolve<HoneyBoxDbContext>();
            var logger = c.Resolve<ILogger<WarehouseService>>();
            var logisticsService = c.Resolve<ILogisticsService>();
            var wechatPayService = c.Resolve<IWechatPayService>();
            return new WarehouseService(dbContext, logger, logisticsService, wechatPayService);
        }).As<IWarehouseService>().InstancePerLifetimeScope();

        // ========== 支付系统服务注册 ==========

        // 注册微信支付配置服务（从数据库读取配置）
        builder.Register(c =>
        {
            var dbContext = c.Resolve<HoneyBoxDbContext>();
            var redisService = c.Resolve<IRedisService>();
            var logger = c.Resolve<ILogger<WechatPayConfigService>>();
            return new WechatPayConfigService(dbContext, redisService, logger);
        }).As<IWechatPayConfigService>().InstancePerLifetimeScope();

        // 注册微信支付 V3 服务
        builder.Register(c =>
        {
            var dbContext = c.Resolve<HoneyBoxDbContext>();
            var httpClientFactory = c.Resolve<System.Net.Http.IHttpClientFactory>();
            var logger = c.Resolve<ILogger<WechatPayV3Service>>();
            var configService = c.Resolve<IWechatPayConfigService>();
            return new WechatPayV3Service(dbContext, httpClientFactory.CreateClient(), logger, configService);
        }).As<IWechatPayV3Service>().InstancePerLifetimeScope();

        // 注册微信支付服务 (V2)，支持版本路由到 V3
        builder.Register(c =>
        {
            var dbContext = c.Resolve<HoneyBoxDbContext>();
            var httpClientFactory = c.Resolve<System.Net.Http.IHttpClientFactory>();
            var logger = c.Resolve<ILogger<WechatPayService>>();
            var configService = c.Resolve<IWechatPayConfigService>();
            var wechatService = c.Resolve<IWechatService>();
            var redisService = c.Resolve<IRedisService>();
            var settings = c.Resolve<Microsoft.Extensions.Options.IOptions<WechatPaySettings>>();
            var appSettings = c.Resolve<AppSettings>();
            // Autofac 原生支持 Lazy<T>，直接解析即可
            var v3ServiceLazy = c.Resolve<Lazy<IWechatPayV3Service>>();
            return new WechatPayService(dbContext, httpClientFactory.CreateClient(), logger, configService, wechatService, redisService, settings, appSettings, v3ServiceLazy);
        }).As<IWechatPayService>().InstancePerLifetimeScope();

        // 注册支付服务
        builder.Register(c =>
        {
            var dbContext = c.Resolve<HoneyBoxDbContext>();
            var logger = c.Resolve<ILogger<PaymentService>>();
            return new PaymentService(dbContext, logger);
        }).As<IPaymentService>().InstancePerLifetimeScope();

        // 注册支付回调服务
        builder.Register(c =>
        {
            var dbContext = c.Resolve<HoneyBoxDbContext>();
            var wechatPayService = c.Resolve<IWechatPayService>();
            var wechatPayV3Service = c.Resolve<IWechatPayV3Service>();
            var wechatPayConfigService = c.Resolve<IWechatPayConfigService>();
            var paymentService = c.Resolve<IPaymentService>();
            var lotteryEngine = c.Resolve<ILotteryEngine>();
            var wechatService = c.Resolve<IWechatService>();
            var redisService = c.Resolve<IRedisService>();
            var logger = c.Resolve<ILogger<PaymentNotifyService>>();
            return new PaymentNotifyService(dbContext, wechatPayService, wechatPayV3Service, wechatPayConfigService, paymentService, lotteryEngine, wechatService, redisService, logger);
        }).As<IPaymentNotifyService>().InstancePerLifetimeScope();

        // 注册充值服务
        builder.Register(c =>
        {
            var dbContext = c.Resolve<HoneyBoxDbContext>();
            var wechatPayService = c.Resolve<IWechatPayService>();
            var paymentService = c.Resolve<IPaymentService>();
            var logger = c.Resolve<ILogger<RechargeService>>();
            return new RechargeService(dbContext, wechatPayService, paymentService, logger);
        }).As<IRechargeService>().InstancePerLifetimeScope();

        // ========== 配置系统服务注册 ==========

        // 注册配置服务
        builder.Register(c =>
        {
            var dbContext = c.Resolve<HoneyBoxDbContext>();
            var logger = c.Resolve<ILogger<ConfigService>>();
            var redisService = c.Resolve<IRedisService>();
            return new ConfigService(dbContext, logger, redisService);
        }).As<IConfigService>().InstancePerLifetimeScope();

        // 注册悬浮球服务
        builder.Register(c =>
        {
            var dbContext = c.Resolve<HoneyBoxDbContext>();
            var logger = c.Resolve<ILogger<FloatBallService>>();
            return new FloatBallService(dbContext, logger);
        }).As<IFloatBallService>().InstancePerLifetimeScope();

        // ========== 签到系统服务注册 ==========

        // 注册签到服务
        builder.Register(c =>
        {
            var dbContext = c.Resolve<HoneyBoxDbContext>();
            var redisService = c.Resolve<IRedisService>();
            var logger = c.Resolve<ILogger<SignService>>();
            return new SignService(dbContext, redisService, logger);
        }).As<ISignService>().InstancePerLifetimeScope();

        // ========== 权益系统服务注册 ==========

        // 注册权益服务
        builder.Register(c =>
        {
            var dbContext = c.Resolve<HoneyBoxDbContext>();
            var logger = c.Resolve<ILogger<QuanYiService>>();
            return new QuanYiService(dbContext, logger);
        }).As<IQuanYiService>().InstancePerLifetimeScope();

        // ========== 商城系统服务注册 ==========

        // 注册商城服务
        builder.Register(c =>
        {
            var dbContext = c.Resolve<HoneyBoxDbContext>();
            var wechatPayService = c.Resolve<IWechatPayService>();
            var logger = c.Resolve<ILogger<MallService>>();
            return new MallService(dbContext, wechatPayService, logger);
        }).As<IMallService>().InstancePerLifetimeScope();

        // ========== 公告系统服务注册 ==========

        // 注册中奖公告服务（用户端）
        builder.Register(c =>
        {
            var dbContext = c.Resolve<HoneyBoxDbContext>();
            var logger = c.Resolve<ILogger<PrizeAnnouncementService>>();
            return new PrizeAnnouncementService(dbContext, logger);
        }).As<IPrizeAnnouncementService>().InstancePerLifetimeScope();
    }
}
