package services

import (
	"ai_scheduler/internal/biz"
	"ai_scheduler/internal/biz/do"
	dingtalk2 "ai_scheduler/internal/biz/handle/dingtalk"
	"ai_scheduler/internal/biz/handle/qywx"
	"ai_scheduler/internal/biz/llm_service"
	"ai_scheduler/internal/biz/llm_service/third_party"
	"ai_scheduler/internal/biz/tools_regis"
	"ai_scheduler/internal/config"
	"ai_scheduler/internal/data/impl"
	"ai_scheduler/internal/domain/component"
	"ai_scheduler/internal/domain/component/callback"
	"ai_scheduler/internal/domain/repo"
	"ai_scheduler/internal/domain/workflow"
	"ai_scheduler/internal/pkg"
	"ai_scheduler/internal/pkg/dingtalk"
	"ai_scheduler/internal/pkg/lsxd"
	"ai_scheduler/internal/pkg/utils_ollama"
	"ai_scheduler/internal/pkg/utils_oss"
	"ai_scheduler/internal/pkg/utils_vllm"

	"ai_scheduler/internal/tools"
	"ai_scheduler/utils"
	"context"
	"testing"

	"github.com/gofiber/fiber/v2/log"
)

func Test_Report(t *testing.T) {
	Run()
	a := cronService.CronReportSendDingTalk(context.Background())
	t.Log(a)
}

func Test_Report_QYWX(t *testing.T) {
	Run()
	a := cronService.CronReportSendQywx(context.Background())
	t.Log(a)
}

var (
	configConfig   *config.Config
	err            error
	dingBotService *DingBotService
	cronService    *CronService
)

// run 函数是程序的入口函数，负责初始化和配置各个组件
func Run() {
	// 加载测试配置
	// configConfig, err = config.LoadConfigWithTest()
	configConfig, err = config.LoadConfigWithEnv()
	// 初始化数据库连接
	db, _ := utils.NewGormDb(configConfig)
	// 初始化各种实现层组件
	sysImpl := impl.NewSysImpl(db)
	taskImpl := impl.NewTaskImpl(db)
	chatHisImpl := impl.NewChatHisImpl(db)
	sessionImpl := impl.NewSessionImpl(db)
	botConfigImpl := impl.NewBotConfigImpl(db)
	botGroupImpl := impl.NewBotGroupImpl(db)
	botUserImpl := impl.NewBotUserImpl(db)
	reportDailyCacheImpl := impl.NewReportDailyCacheImpl(db)
	// 初始化Do业务对象
	doDo := do.NewDo(sessionImpl, sysImpl, taskImpl, chatHisImpl, configConfig)
	// 初始化Ollama客户端
	client, _, _ := utils_ollama.NewClient(configConfig)
	// 初始化vLLM客户端
	utils_vllmClient, _, _ := utils_vllm.NewClient(configConfig)
	// 初始化Redis数据库连接
	rdb := utils.NewRdb(configConfig)
	// 初始化仓库层
	repos := repo.NewRepos(sessionImpl, configConfig, rdb)
	// 初始化包级别的Redis连接
	pkgRdb := pkg.NewRdb(configConfig)

	// 初始化机器人工具实现层
	botToolsImpl := impl.NewBotToolsImpl(db)
	// 初始化机器人部门实现层
	botDeptImpl := impl.NewBotDeptImpl(db)
	// 初始化Redis管理器
	redisManager := callback.NewRedisManager(pkgRdb)
	// 初始化登录组件
	lsxdLogin := lsxd.NewLogin(configConfig, rdb)
	// 初始化组件
	components := component.NewComponents(redisManager, lsxdLogin)
	// 初始化工作流注册表
	registry := workflow.NewRegistry(configConfig, client, repos, components)
	// 初始化钉钉旧版客户端
	oldClient := dingtalk.NewOldClient(configConfig)
	// 初始化Ollama服务
	ollamaService := llm_service.NewOllamaGenerate(client, utils_vllmClient, configConfig, chatHisImpl)
	// 初始化工具管理器
	manager := tools.NewManager(configConfig, client, rdb)
	// 初始化钉钉联系人客户端
	contactClient, _ := dingtalk.NewContactClient(configConfig)
	// 初始化钉钉记事本客户端
	notableClient, _ := dingtalk.NewNotableClient(configConfig)
	// 初始化工具注册
	toolRegis := tools_regis.NewToolsRegis(botToolsImpl)
	// 初始化机器人聊天历史实现层
	botChatHisImpl := impl.NewBotChatHisImpl(db)
	// 初始化钉钉认证
	auth := dingtalk2.NewAuth(configConfig, rdb, botConfigImpl)
	// 初始化部门服务
	dept := dingtalk2.NewDept(botDeptImpl, auth)
	// 初始化用户服务
	user := dingtalk2.NewUser(botUserImpl, auth, dept)
	// 初始化发送卡片客户端
	sendCardClient := dingtalk2.NewSendCardClient(auth, log.DefaultLogger())
	// 初始化处理器
	handle := do.NewHandle(ollamaService, manager, configConfig, sessionImpl, registry, oldClient, contactClient, notableClient)
	// 初始化钉钉机器人业务逻辑
	utils_ossClient, _ := utils_oss.NewClient(configConfig)
	// 初始化工作流管理器
	botGroupConfigImpl := impl.NewBotGroupConfigImpl(db)
	botGroupQywxImpl := impl.NewBotGroupQywxImpl(db)
	qywxAuth := qywx.NewAuth(configConfig, rdb)
	macro := do.NewMacro(botGroupImpl, reportDailyCacheImpl, configConfig, rdb)
	group := qywx.NewGroup(botGroupQywxImpl, qywxAuth)
	other := qywx.NewOther(qywxAuth)
	qywxAppBiz := biz.NewQywxAppBiz(configConfig, botGroupQywxImpl, group, other)
	reportDailyCacheImpl := impl.NewReportDailyCacheImpl(db)
	macro := do.NewMacro(botGroupImpl, reportDailyCacheImpl, configConfig, rdb)
	groupConfigBiz := biz.NewGroupConfigBiz(toolRegis, utils_ossClient, botGroupConfigImpl, registry, configConfig, impl.NewReportDailyCacheImpl(db), rdb, macro, manager, handle)
	dingTalkBotBiz := biz.NewDingTalkBotBiz(doDo, handle, botConfigImpl, botGroupImpl, user, botChatHisImpl, reportDailyCacheImpl, manager, configConfig, sendCardClient, groupConfigBiz, macro)
	// 初始化钉钉机器人服务
	cronService = NewCronService(configConfig, dingTalkBotBiz, qywxAppBiz, groupConfigBiz)
	hsyq := third_party.NewHsyq()
	advicerbiz := biz.NewAdviceBiz(hsyq)
	advicer = NewAdviceService(advicerbiz, configConfig)
}
