package router

import (
	"net/http"
	"testing"

	"github.com/go-chi/chi/v5"
	"github.com/pingcap/agent-git-service/internal/rest"
)

func TestRepositorySyncContractRoutesAreRegistered(t *testing.T) {
	routes := chi.NewRouter()
	registerRepositorySyncRoutes(routes, &rest.Deps{})

	registered := map[string]map[string]bool{}
	if err := chi.Walk(routes, func(method, route string, _ http.Handler, _ ...func(http.Handler) http.Handler) error {
		if registered[route] == nil {
			registered[route] = map[string]bool{}
		}
		registered[route][method] = true
		return nil
	}); err != nil {
		t.Fatalf("walk repository sync routes: %v", err)
	}

	required := map[string][]string{
		"/api/ext/v1/sync/github/authorize/start":        {http.MethodPost},
		"/api/ext/v1/sync/github/install/start":          {http.MethodPost},
		"/api/ext/v1/sync/github/installations":          {http.MethodGet},
		"/api/ext/v1/sync/github/account":                {http.MethodDelete},
		"/api/ext/v1/sync/github/repositories":           {http.MethodGet},
		"/api/ext/v1/sync/overview":                      {http.MethodGet},
		"/api/ext/v1/sync/bindings":                      {http.MethodPost, http.MethodGet},
		"/api/ext/v1/sync/bindings/{binding_id}":         {http.MethodGet, http.MethodPatch},
		"/api/ext/v1/sync/bindings/{binding_id}/runs":    {http.MethodPost, http.MethodGet},
		"/api/ext/v1/sync/bindings/{binding_id}/handoff": {http.MethodPost},
		"/api/ext/v1/sync/runs":                          {http.MethodGet},
		"/api/ext/v1/sync/runs/{run_id}":                 {http.MethodGet},
		"/api/ext/v1/sync/runs/{run_id}/items":           {http.MethodGet},
		"/api/ext/v1/sync/runs/{run_id}/cancel":          {http.MethodPost},
		"/api/ext/v1/sync/runs/{run_id}/recover":         {http.MethodPost},
	}
	for route, methods := range required {
		for _, method := range methods {
			if !registered[route][method] {
				t.Fatalf("required repository sync route %s %s is not registered", method, route)
			}
		}
	}
}
