package channel

import (
	"testing"

	"github.com/gin-gonic/gin"
	"github.com/stretchr/testify/require"

	"github.com/tidbcloud/AgentTeam/internal/api/testkit/httputil"
)

func TestRegisterRoutesOwnsChannelAndThreadRoutes(t *testing.T) {
	t.Parallel()

	router := gin.New()
	RegisterRoutes(router, Dependencies{})

	counts := httputil.RouteCounts(router)
	for _, route := range channelRouteKeys() {
		require.Equal(t, 1, counts[route], route)
	}
	for _, route := range threadRouteKeys() {
		require.Equal(t, 1, counts[route], route)
	}
	for _, route := range legacyNotificationRouteKeys() {
		require.Zero(t, counts[route], route)
	}
}

func legacyNotificationRouteKeys() []string {
	const p = "/api/v1/workspaces/:workspaceId"
	return []string{
		"GET " + p + "/channels/:channelId/notifications",
		"POST " + p + "/channels/:channelId/notifications/self",
		"POST " + p + "/channels/:channelId/notifications/agents/:agentId",
		"GET " + p + "/threads/:threadChannelId/notifications",
		"POST " + p + "/threads/:threadChannelId/notifications/self",
		"POST " + p + "/threads/:threadChannelId/notifications/agents/:agentId",
	}
}

func channelRouteKeys() []string {
	const p = "/api/v1/workspaces/:workspaceId"
	return []string{
		"GET " + p + "/channels",
		"POST " + p + "/channels",
		"GET " + p + "/channels/discover",
		"GET " + p + "/channels/discover/:channelId",
		"PATCH " + p + "/channels/:channelId",
		"DELETE " + p + "/channels/:channelId",
		"POST " + p + "/channels/:channelId/owner",
		"POST " + p + "/channels/:channelId/join",
		"POST " + p + "/channels/:channelId/archive",
		"POST " + p + "/channels/:channelId/unarchive",
		"POST " + p + "/channels/:channelId/leave",
		"GET " + p + "/channels/dm",
		"POST " + p + "/channels/dm",
		"GET " + p + "/channels/dm/:channelId",
		"POST " + p + "/channels/dm/:channelId/hide",
		"POST " + p + "/channels/:channelId/owner-context-dismiss",
		"GET " + p + "/channels/:channelId/addable-agents",
		"GET " + p + "/channels/:channelId/addable-people",
		"GET " + p + "/channels/:channelId/members",
		"POST " + p + "/channels/:channelId/members",
		"POST " + p + "/channels/:channelId/members/batch",
		"POST " + p + "/channels/:channelId/agent-task-summaries",
		"PATCH " + p + "/channels/:channelId/members/agent/:agentId",
		"DELETE " + p + "/channels/:channelId/members/agent/:agentId",
		"DELETE " + p + "/channels/:channelId/members/service/:memberId",
		"DELETE " + p + "/channels/:channelId/members/user/:userId",
	}
}

func threadRouteKeys() []string {
	const p = "/api/v1/workspaces/:workspaceId"
	return []string{
		"GET " + p + "/channels/:channelId/threads",
		"GET " + p + "/threads/count",
		"GET " + p + "/threads",
		"GET " + p + "/threads/:threadChannelId/membership",
		"GET " + p + "/threads/:threadChannelId",
		"POST " + p + "/channels/:channelId/threads",
		"POST " + p + "/threads/:threadChannelId/subscribe",
		"POST " + p + "/threads/:threadChannelId/unsubscribe",
		"GET " + p + "/threads/:threadChannelId/attachments",
		"GET " + p + "/threads/:threadChannelId/review-contexts",
		"GET " + p + "/threads/:threadChannelId/review/snapshot",
		"GET " + p + "/threads/:threadChannelId/review/summary",
		"GET " + p + "/threads/:threadChannelId/review/files",
		"GET " + p + "/threads/:threadChannelId/review/fingerprint",
		"GET " + p + "/threads/:threadChannelId/review/diff",
	}
}
