package httpapi

import (
	"net/http"

	"cmr-backend/internal/httpapi/handlers"
	"cmr-backend/internal/httpapi/middleware"
	"cmr-backend/internal/platform/jwtx"
	"cmr-backend/internal/service"
)

func NewRouter(
	appEnv string,
	jwtManager *jwtx.Manager,
	authService *service.AuthService,
	entryService *service.EntryService,
	entryHomeService *service.EntryHomeService,
	adminResourceService *service.AdminResourceService,
	adminProductionService *service.AdminProductionService,
	adminEventService *service.AdminEventService,
	adminPipelineService *service.AdminPipelineService,
	eventService *service.EventService,
	eventPlayService *service.EventPlayService,
	configService *service.ConfigService,
	homeService *service.HomeService,
	profileService *service.ProfileService,
	resultService *service.ResultService,
	sessionService *service.SessionService,
	devService *service.DevService,
	meService *service.MeService,
) http.Handler {
	mux := http.NewServeMux()

	healthHandler := handlers.NewHealthHandler()
	authHandler := handlers.NewAuthHandler(authService)
	entryHandler := handlers.NewEntryHandler(entryService)
	entryHomeHandler := handlers.NewEntryHomeHandler(entryHomeService)
	adminResourceHandler := handlers.NewAdminResourceHandler(adminResourceService)
	adminProductionHandler := handlers.NewAdminProductionHandler(adminProductionService)
	adminEventHandler := handlers.NewAdminEventHandler(adminEventService)
	adminPipelineHandler := handlers.NewAdminPipelineHandler(adminPipelineService)
	eventHandler := handlers.NewEventHandler(eventService)
	eventPlayHandler := handlers.NewEventPlayHandler(eventPlayService)
	configHandler := handlers.NewConfigHandler(configService)
	homeHandler := handlers.NewHomeHandler(homeService)
	profileHandler := handlers.NewProfileHandler(profileService)
	resultHandler := handlers.NewResultHandler(resultService)
	sessionHandler := handlers.NewSessionHandler(sessionService)
	devHandler := handlers.NewDevHandler(devService)
	meHandler := handlers.NewMeHandler(meService)
	authMiddleware := middleware.NewAuthMiddleware(jwtManager)

	mux.HandleFunc("GET /healthz", healthHandler.Get)
	mux.HandleFunc("GET /home", homeHandler.GetHome)
	mux.HandleFunc("GET /cards", homeHandler.GetCards)
	mux.HandleFunc("GET /entry/resolve", entryHandler.Resolve)
	mux.Handle("GET /admin/maps", authMiddleware(http.HandlerFunc(adminResourceHandler.ListMaps)))
	mux.Handle("POST /admin/maps", authMiddleware(http.HandlerFunc(adminResourceHandler.CreateMap)))
	mux.Handle("GET /admin/maps/{mapPublicID}", authMiddleware(http.HandlerFunc(adminResourceHandler.GetMap)))
	mux.Handle("POST /admin/maps/{mapPublicID}/versions", authMiddleware(http.HandlerFunc(adminResourceHandler.CreateMapVersion)))
	mux.Handle("GET /admin/places", authMiddleware(http.HandlerFunc(adminProductionHandler.ListPlaces)))
	mux.Handle("POST /admin/places", authMiddleware(http.HandlerFunc(adminProductionHandler.CreatePlace)))
	mux.Handle("GET /admin/places/{placePublicID}", authMiddleware(http.HandlerFunc(adminProductionHandler.GetPlace)))
	mux.Handle("POST /admin/places/{placePublicID}/map-assets", authMiddleware(http.HandlerFunc(adminProductionHandler.CreateMapAsset)))
	mux.Handle("GET /admin/map-assets/{mapAssetPublicID}", authMiddleware(http.HandlerFunc(adminProductionHandler.GetMapAsset)))
	mux.Handle("POST /admin/map-assets/{mapAssetPublicID}/tile-releases", authMiddleware(http.HandlerFunc(adminProductionHandler.CreateTileRelease)))
	mux.Handle("POST /admin/map-assets/{mapAssetPublicID}/course-sets", authMiddleware(http.HandlerFunc(adminProductionHandler.CreateCourseSet)))
	mux.Handle("GET /admin/course-sources", authMiddleware(http.HandlerFunc(adminProductionHandler.ListCourseSources)))
	mux.Handle("POST /admin/course-sources", authMiddleware(http.HandlerFunc(adminProductionHandler.CreateCourseSource)))
	mux.Handle("GET /admin/course-sources/{sourcePublicID}", authMiddleware(http.HandlerFunc(adminProductionHandler.GetCourseSource)))
	mux.Handle("GET /admin/course-sets/{courseSetPublicID}", authMiddleware(http.HandlerFunc(adminProductionHandler.GetCourseSet)))
	mux.Handle("POST /admin/course-sets/{courseSetPublicID}/variants", authMiddleware(http.HandlerFunc(adminProductionHandler.CreateCourseVariant)))
	mux.Handle("GET /admin/runtime-bindings", authMiddleware(http.HandlerFunc(adminProductionHandler.ListRuntimeBindings)))
	mux.Handle("POST /admin/runtime-bindings", authMiddleware(http.HandlerFunc(adminProductionHandler.CreateRuntimeBinding)))
	mux.Handle("GET /admin/runtime-bindings/{runtimeBindingPublicID}", authMiddleware(http.HandlerFunc(adminProductionHandler.GetRuntimeBinding)))
	mux.Handle("GET /admin/playfields", authMiddleware(http.HandlerFunc(adminResourceHandler.ListPlayfields)))
	mux.Handle("POST /admin/playfields", authMiddleware(http.HandlerFunc(adminResourceHandler.CreatePlayfield)))
	mux.Handle("GET /admin/playfields/{playfieldPublicID}", authMiddleware(http.HandlerFunc(adminResourceHandler.GetPlayfield)))
	mux.Handle("POST /admin/playfields/{playfieldPublicID}/versions", authMiddleware(http.HandlerFunc(adminResourceHandler.CreatePlayfieldVersion)))
	mux.Handle("GET /admin/resource-packs", authMiddleware(http.HandlerFunc(adminResourceHandler.ListResourcePacks)))
	mux.Handle("POST /admin/resource-packs", authMiddleware(http.HandlerFunc(adminResourceHandler.CreateResourcePack)))
	mux.Handle("GET /admin/resource-packs/{resourcePackPublicID}", authMiddleware(http.HandlerFunc(adminResourceHandler.GetResourcePack)))
	mux.Handle("POST /admin/resource-packs/{resourcePackPublicID}/versions", authMiddleware(http.HandlerFunc(adminResourceHandler.CreateResourcePackVersion)))
	mux.Handle("GET /admin/events", authMiddleware(http.HandlerFunc(adminEventHandler.ListEvents)))
	mux.Handle("POST /admin/events", authMiddleware(http.HandlerFunc(adminEventHandler.CreateEvent)))
	mux.Handle("GET /admin/events/{eventPublicID}", authMiddleware(http.HandlerFunc(adminEventHandler.GetEvent)))
	mux.Handle("PUT /admin/events/{eventPublicID}", authMiddleware(http.HandlerFunc(adminEventHandler.UpdateEvent)))
	mux.Handle("POST /admin/events/{eventPublicID}/source", authMiddleware(http.HandlerFunc(adminEventHandler.SaveSource)))
	mux.Handle("GET /admin/events/{eventPublicID}/presentations", authMiddleware(http.HandlerFunc(adminEventHandler.ListPresentations)))
	mux.Handle("POST /admin/events/{eventPublicID}/presentations", authMiddleware(http.HandlerFunc(adminEventHandler.CreatePresentation)))
	mux.Handle("POST /admin/events/{eventPublicID}/presentations/import", authMiddleware(http.HandlerFunc(adminEventHandler.ImportPresentation)))
	mux.Handle("GET /admin/presentations/{presentationPublicID}", authMiddleware(http.HandlerFunc(adminEventHandler.GetPresentation)))
	mux.Handle("GET /admin/events/{eventPublicID}/content-bundles", authMiddleware(http.HandlerFunc(adminEventHandler.ListContentBundles)))
	mux.Handle("POST /admin/events/{eventPublicID}/content-bundles", authMiddleware(http.HandlerFunc(adminEventHandler.CreateContentBundle)))
	mux.Handle("POST /admin/events/{eventPublicID}/content-bundles/import", authMiddleware(http.HandlerFunc(adminEventHandler.ImportContentBundle)))
	mux.Handle("GET /admin/content-bundles/{contentBundlePublicID}", authMiddleware(http.HandlerFunc(adminEventHandler.GetContentBundle)))
	mux.Handle("POST /admin/events/{eventPublicID}/defaults", authMiddleware(http.HandlerFunc(adminEventHandler.UpdateEventDefaults)))
	mux.Handle("GET /admin/events/{eventPublicID}/pipeline", authMiddleware(http.HandlerFunc(adminPipelineHandler.GetEventPipeline)))
	mux.Handle("POST /admin/sources/{sourceID}/build", authMiddleware(http.HandlerFunc(adminPipelineHandler.BuildSource)))
	mux.Handle("GET /admin/builds/{buildID}", authMiddleware(http.HandlerFunc(adminPipelineHandler.GetBuild)))
	mux.Handle("POST /admin/builds/{buildID}/publish", authMiddleware(http.HandlerFunc(adminPipelineHandler.PublishBuild)))
	mux.Handle("GET /admin/releases/{releasePublicID}", authMiddleware(http.HandlerFunc(adminPipelineHandler.GetRelease)))
	mux.Handle("POST /admin/releases/{releasePublicID}/runtime-binding", authMiddleware(http.HandlerFunc(adminPipelineHandler.BindReleaseRuntime)))
	mux.Handle("POST /admin/events/{eventPublicID}/rollback", authMiddleware(http.HandlerFunc(adminPipelineHandler.RollbackRelease)))
	if appEnv != "production" {
		mux.HandleFunc("GET /dev/workbench", devHandler.Workbench)
		mux.HandleFunc("POST /dev/bootstrap-demo", devHandler.BootstrapDemo)
		mux.HandleFunc("POST /dev/client-logs", devHandler.CreateClientLog)
		mux.HandleFunc("GET /dev/client-logs", devHandler.ListClientLogs)
		mux.HandleFunc("DELETE /dev/client-logs", devHandler.ClearClientLogs)
		mux.HandleFunc("GET /dev/manifest-summary", devHandler.ManifestSummary)
		mux.HandleFunc("GET /dev/demo-assets/presentations/{demoKey}", devHandler.DemoPresentationSchema)
		mux.HandleFunc("GET /dev/demo-assets/content-manifests/{demoKey}", devHandler.DemoContentManifest)
		mux.HandleFunc("GET /dev/config/local-files", configHandler.ListLocalFiles)
		mux.HandleFunc("POST /dev/events/{eventPublicID}/config-sources/import-local", configHandler.ImportLocal)
		mux.HandleFunc("POST /dev/config-builds/preview", configHandler.BuildPreview)
		mux.HandleFunc("POST /dev/config-builds/publish", configHandler.PublishBuild)
	}
	mux.Handle("GET /me/entry-home", authMiddleware(http.HandlerFunc(entryHomeHandler.Get)))
	mux.Handle("GET /me/profile", authMiddleware(http.HandlerFunc(profileHandler.Get)))
	mux.HandleFunc("GET /events/{eventPublicID}", eventHandler.GetDetail)
	mux.Handle("GET /events/{eventPublicID}/play", authMiddleware(http.HandlerFunc(eventPlayHandler.Get)))
	mux.Handle("GET /events/{eventPublicID}/config-sources", authMiddleware(http.HandlerFunc(configHandler.ListSources)))
	mux.Handle("POST /events/{eventPublicID}/launch", authMiddleware(http.HandlerFunc(eventHandler.Launch)))
	mux.Handle("GET /config-sources/{sourceID}", authMiddleware(http.HandlerFunc(configHandler.GetSource)))
	mux.Handle("GET /config-builds/{buildID}", authMiddleware(http.HandlerFunc(configHandler.GetBuild)))
	mux.Handle("GET /sessions/{sessionPublicID}", authMiddleware(http.HandlerFunc(sessionHandler.GetDetail)))
	mux.Handle("GET /sessions/{sessionPublicID}/result", authMiddleware(http.HandlerFunc(resultHandler.GetSessionResult)))
	mux.HandleFunc("POST /sessions/{sessionPublicID}/start", sessionHandler.Start)
	mux.HandleFunc("POST /sessions/{sessionPublicID}/finish", sessionHandler.Finish)
	mux.HandleFunc("POST /auth/sms/send", authHandler.SendSMSCode)
	mux.HandleFunc("POST /auth/login/sms", authHandler.LoginSMS)
	mux.HandleFunc("POST /auth/login/wechat-mini", authHandler.LoginWechatMini)
	mux.Handle("POST /auth/bind/mobile", authMiddleware(http.HandlerFunc(authHandler.BindMobile)))
	mux.HandleFunc("POST /auth/refresh", authHandler.Refresh)
	mux.HandleFunc("POST /auth/logout", authHandler.Logout)
	mux.Handle("GET /me", authMiddleware(http.HandlerFunc(meHandler.Get)))
	mux.Handle("GET /me/sessions", authMiddleware(http.HandlerFunc(sessionHandler.ListMine)))
	mux.Handle("GET /me/results", authMiddleware(http.HandlerFunc(resultHandler.ListMine)))

	return mux
}
