package content

import (
	"net/http"

	"github.com/tidbcloud/AgentTeam/internal/api/dto"
	"github.com/tidbcloud/AgentTeam/internal/api/meshxapi"
)

type routeRegistrar interface {
	Handle(pattern string, handler http.Handler)
}
type RouteMiddleware func(http.Handler) http.Handler

func RegisterRoutes(router routeRegistrar, handler *Handler, middleware RouteMiddleware) {
	if router == nil || handler == nil {
		return
	}
	register := func(pattern string, route http.Handler) {
		if middleware != nil {
			route = middleware(route)
		}
		router.Handle(pattern, route)
	}
	register("POST /api/v1/content-store/contents", meshxapi.HandleAuthJSONLimit[dto.ContentStoreCreateRequest](64<<10, handler.Create, errorHandling()))
	register("GET /api/v1/content-store/contents/{contentId}", meshxapi.HandleAuth(handler.Get, errorHandling()))
	register("POST /api/v1/content-store/contents/{contentId}/versions", meshxapi.HandleAuthJSONLimit[dto.ContentStoreCommitRequest](maxCommitBodyBytes, handler.Commit, errorHandling()))
	register("GET /api/v1/content-store/contents/{contentId}/versions", meshxapi.HandleAuth(handler.ListVersions, errorHandling()))
	register("GET /api/v1/content-store/contents/{contentId}/versions/{versionId}", meshxapi.HandleAuth(handler.GetVersion, errorHandling()))
	register("POST /api/v1/content-store/contents/{contentId}/pins", meshxapi.HandleAuthJSONLimit[dto.ContentStorePinCreateRequest](64<<10, handler.CreatePin, errorHandling()))
	register("GET /api/v1/content-store/contents/{contentId}/pins", meshxapi.HandleAuth(handler.ListPins, errorHandling()))
	register("DELETE /api/v1/content-store/contents/{contentId}/pins/{pinId}", meshxapi.HandleAuth(handler.DeletePin, errorHandling()))
	register("GET /api/v1/content-store/contents/{contentId}/path-history", meshxapi.HandleAuth(handler.PathHistory, errorHandling()))
	register("GET /api/v1/content-store/contents/{contentId}/compare", meshxapi.HandleAuth(handler.Compare, errorHandling()))
	register("GET /api/v1/content-store/contents/{contentId}/versions/{versionId}/files/{path...}", http.HandlerFunc(handler.OpenFile))
	register("GET /api/v1/content-store/contents/{contentId}/versions/{versionId}/archive", http.HandlerFunc(handler.OpenArchive))
}
