using CncRepository.Impl;
using CncRepository.Impl.Dashboard;
using CncRepository.Impl.Log;
using CncService.Impl;

namespace CncService.Tests
{
    /// <summary>
    /// Service工厂 —— 封装Repository创建，提供所有Service实例化方法
    /// 所有Service使用真实数据库连接串，进行集成测试
    /// </summary>
    public static class ServiceFactory
    {
        private static readonly string ConnStr = TestDb.ConnectionString;
        private static readonly string ConnStrLog = TestDb.ConnectionString;

        // ======== Repository 创建（业务库） ========
        private static WorkshopRepository NewWorkshopRepo() => new WorkshopRepository(ConnStr);
        private static BrandRepository NewBrandRepo() => new BrandRepository(ConnStr);
        private static BrandFieldMappingRepository NewMappingRepo() => new BrandFieldMappingRepository(ConnStr);
        private static CollectAddressRepository NewCollectAddressRepo() => new CollectAddressRepository(ConnStr);
        private static MachineRepository NewMachineRepo() => new MachineRepository(ConnStr);
        private static WorkerRepository NewWorkerRepo() => new WorkerRepository(ConnStr);
        private static WorkerMachineRepository NewWorkerMachineRepo() => new WorkerMachineRepository(ConnStr);
        private static SysConfigRepository NewSysConfigRepo() => new SysConfigRepository(ConnStr);
        private static AlertRepository NewAlertRepo() => new AlertRepository(ConnStr);
        private static DailyProductionRepository NewDailyProductionRepo() => new DailyProductionRepository(ConnStr);
        private static ProductionSegmentRepository NewProductionSegmentRepo() => new ProductionSegmentRepository(ConnStr);
        private static ProductionAdjustmentRepository NewProductionAdjustmentRepo() => new ProductionAdjustmentRepository(ConnStr);
        private static ScreenConfigRepository NewScreenConfigRepo() => new ScreenConfigRepository(ConnStr);
        private static ScreenFilterRepository NewScreenFilterRepo() => new ScreenFilterRepository(ConnStr);
        private static DashboardRepository NewDashboardRepo() => new DashboardRepository(ConnStr);
        private static SystemLogRepository NewSystemLogRepo() => new SystemLogRepository(ConnStrLog);

        // ======== Repository 创建（日志库） ========
        private static CollectorHeartbeatRepository NewCollectorHeartbeatRepo() => new CollectorHeartbeatRepository(ConnStrLog);
        private static CollectRawRepository NewCollectRawRepo() => new CncRepository.Impl.Log.CollectRawRepository(ConnStrLog);

        // ======== Service 创建 ========

        /// <summary>创建AuthService，需指定JWT密钥</summary>
        public static AuthService CreateAuthService(string jwtSecret = "test-jwt-secret-key-for-unit-testing-2024")
        {
            return new AuthService(NewSysConfigRepo(), jwtSecret);
        }

        /// <summary>创建WorkshopService</summary>
        public static WorkshopService CreateWorkshopService()
        {
            return new WorkshopService(NewWorkshopRepo());
        }

        /// <summary>创建BrandService</summary>
        public static BrandService CreateBrandService()
        {
            return new BrandService(NewBrandRepo(), NewMappingRepo(), NewCollectAddressRepo());
        }

        /// <summary>创建MachineService</summary>
        public static MachineService CreateMachineService()
        {
            return new MachineService(NewMachineRepo(), NewCollectAddressRepo(), NewWorkerMachineRepo(), NewBrandRepo());
        }

        /// <summary>创建CollectAddressService</summary>
        public static CollectAddressService CreateCollectAddressService()
        {
            return new CollectAddressService(NewCollectAddressRepo(), NewMachineRepo(), NewBrandRepo(), NewWorkshopRepo(), NewCollectRawRepo(), NewSysConfigRepo());
        }

        /// <summary>创建WorkerService</summary>
        public static WorkerService CreateWorkerService()
        {
            return new WorkerService(NewWorkerRepo(), NewWorkerMachineRepo(), NewMachineRepo(), NewSysConfigRepo());
        }

        /// <summary>创建ProductionService</summary>
        public static ProductionService CreateProductionService()
        {
            return new ProductionService(NewDailyProductionRepo(), NewProductionSegmentRepo(), NewProductionAdjustmentRepo());
        }

        /// <summary>创建AlertService</summary>
        public static AlertService CreateAlertService()
        {
            return new AlertService(NewAlertRepo());
        }

        /// <summary>创建ScreenService</summary>
        public static ScreenService CreateScreenService()
        {
            return new ScreenService(NewScreenConfigRepo(), NewScreenFilterRepo(), NewWorkshopRepo());
        }

        /// <summary>创建SystemLogService</summary>
        public static SystemLogService CreateSystemLogService()
        {
            return new SystemLogService(NewSystemLogRepo());
        }

        /// <summary>创建DashboardService</summary>
        public static DashboardService CreateDashboardService()
        {
            return new DashboardService(NewDashboardRepo(), NewCollectorHeartbeatRepo(), NewSysConfigRepo());
        }

        /// <summary>创建CollectDataService</summary>
        public static CollectDataService CreateCollectDataService()
        {
            return new CollectDataService(NewCollectRawRepo());
        }
    }
}
