package main

import (
	"log"

	"senlinai-agent/backend/internal/config"
	"senlinai-agent/backend/internal/httpx"
	"senlinai-agent/backend/internal/logic/auth"
	"senlinai-agent/backend/internal/logic/files"
	"senlinai-agent/backend/internal/logic/inbox"
	"senlinai-agent/backend/internal/logic/projects"
	"senlinai-agent/backend/internal/logic/tasks"
	"senlinai-agent/backend/internal/models"
)

func main() {
	cfg := config.Load()

	err := models.New(cfg.DSN)
	if err != nil {
		log.Fatal(err)
	}

	authService := auth.NewService(cfg.AuthSecret)
	projectService := projects.NewService()
	fileService := files.NewService(cfg.StorageDir, models.DBService)
	taskService := tasks.NewService(models.DBService)
	projectHandler := projects.NewHandler(projectService)
	tagHandler := projects.NewTagHandler(projectService)
	cronHandler := projects.NewCronHandler(projectService)
	taskHandler := tasks.NewHandler(taskService)
	fileHandler := files.NewHandler(fileService)
	inboxHandler := inbox.NewHandler(inbox.NewService(inbox.StaticAnalyzer{}))
	authHandler := auth.NewHandler(authService)
	// search 与 AI 当前只有 service，尚无 HTTP registrar；后续功能任务应在实现真实契约后从此处注入，不能注册伪端点。
	appRouter := httpx.NewProtectedRouter(
		cfg,
		authService.VerifySession,
		authHandler,
		projectHandler,
		tagHandler,
		cronHandler,
		taskHandler,
		fileHandler,
		inboxHandler,
	)
	if err := appRouter.Run(":" + cfg.Port); err != nil {
		log.Fatal(err)
	}
}
