package auth

import (
	"testing"

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

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

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

	counts := authRouteCounts(router)
	for _, route := range authRouteKeys() {
		require.Equal(t, 1, counts[route], route)
	}
}

func authRouteKeys() []string {
	return []string{
		"POST /api/v1/auth/login",
		"POST /api/v1/auth/feishu/login-url",
		"POST /api/v1/auth/feishu/login",
		"POST /api/v1/auth/register",
		"POST /api/v1/auth/logout",
		"POST /api/v1/auth/refresh",
		"GET /api/v1/me",
		"PATCH /api/v1/me",
		"GET /api/v1/me/mobile-sessions",
		"DELETE /api/v1/me/mobile-sessions/:sessionFamilyId",
		"GET /api/v1/auth/mobile/history",
		"POST /api/v1/auth/mobile/request-verification",
		"POST /api/v1/auth/mobile/verify",
		"POST /api/v1/auth/verify-email",
		"POST /api/v1/auth/resend-verification",
		"POST /api/v1/auth/forgot-password",
		"POST /api/v1/auth/reset-password",
		"GET /api/v1/auth/invite-info",
		"POST /api/v1/auth/accept-invite",
		"GET /api/v1/auth/oidc/discovery",
		"POST /api/v1/auth/oidc/authorize-url",
		"POST /api/v1/auth/oidc/callback",
	}
}

func authRouteCounts(router *gin.Engine) map[string]int {
	counts := make(map[string]int)
	for _, route := range router.Routes() {
		counts[route.Method+" "+route.Path]++
	}
	return counts
}
