package cmd

import "testing"

func TestConstructDocURL_SupportedLanguages(t *testing.T) {
	tests := []struct {
		language string
		keyword  string
		wantURL  string
	}{
		{"go", "net/http", "https://pkg.go.dev/net/http"},
		{"rust", "tokio", "https://docs.rs/tokio/latest/tokio/"},
		{"python", "asyncio", "https://docs.python.org/3/library/asyncio.html"},
		{"java", "java/util/list", "https://docs.oracle.com/javase/8/docs/api/java/util/list.html"},
		{"spring", "mcp", "https://docs.spring.io/spring-ai/reference/api/mcp/mcp-overview.html"},
		{"typescript", "utility-types", "https://www.typescriptlang.org/docs/handbook/utility-types.html"},
		{"react", "hooks", "https://react.dev/reference/react"},
		{"vue", "essentials/reactivity-fundamentals", "https://vuejs.org/guide/essentials/reactivity-fundamentals.html"},
		{"nuxt", "directory-structure", "https://nuxt.com/docs/guide/directory-structure"},
		{"docker", "compose", "https://docs.docker.com/compose"},
		{"cloudflare", "workers", "https://developers.cloudflare.com/workers"},
		{"astro", "components", "https://docs.astro.build/en/basics/astro-components/"},
		{"csharp", "regex", "https://learn.microsoft.com/en-us/dotnet/standard/base-types/regular-expressions"},
		{"kotlin", "regex", "https://kotlinlang.org/docs/strings.html"},
		{"php", "pcre", "https://www.php.net/manual/en/book.pcre.php"},
		{"ruby", "Regexp", "https://ruby-doc.org/core/Regexp.html"},
		{"elixir", "String", "https://hexdocs.pm/elixir/String.html"},
		{"nextjs", "routing", "https://nextjs.org/docs/app/building-your-application/routing"},
		{"svelte", "kit", "https://svelte.dev/docs/kit"},
		{"angular", "http", "https://angular.dev/guide/http"},
		{"remix", "routes", "https://v2.remix.run/docs/file-conventions/routes"},
		{"solid", "signals", "https://github.com/solidjs/solid-docs"},
		{"express", "routing", "https://expressjs.com/en/guide/routing.html"},
	}

	for _, tt := range tests {
		t.Run(tt.language+"_"+tt.keyword, func(t *testing.T) {
			got, err := constructDocURL(tt.language, tt.keyword)
			if err != nil {
				t.Fatalf("constructDocURL(%q, %q) returned error: %v", tt.language, tt.keyword, err)
			}
			if got != tt.wantURL {
				t.Fatalf("constructDocURL(%q, %q) = %q, want %q", tt.language, tt.keyword, got, tt.wantURL)
			}
		})
	}
}

func TestConstructDocURL_UnsupportedLanguage(t *testing.T) {
	if _, err := constructDocURL("haskell", "regex-tdfa"); err == nil {
		t.Fatal("constructDocURL should return an error for unsupported language")
	}
}

func TestMapLanguageToType(t *testing.T) {
	tests := []struct {
		language string
		wantType string
	}{
		{"go", "godocs"},
		{"golang", "godocs"},
		{"rust", "rustdocs"},
		{"python", "pythondocs"},
		{"py", "pythondocs"},
		{"java", "javadocs"},
		{"spring", "springdocs"},
		{"typescript", "tsdocs"},
		{"ts", "tsdocs"},
		{"react", "reactdocs"},
		{"vue", "vuedocs"},
		{"nuxt", "nuxtdocs"},
		{"docker", "dockerdocs"},
		{"cloudflare", "cloudflaredocs"},
		{"cf", "cloudflaredocs"},
		{"astro", "astrodocs"},
		{"csharp", "url"},
		{"kotlin", "url"},
		{"php", "url"},
		{"ruby", "url"},
		{"elixir", "url"},
		{"nextjs", "url"},
		{"next", "url"},
		{"svelte", "url"},
		{"angular", "url"},
		{"ng", "url"},
		{"remix", "url"},
		{"solidjs", "github"},
		{"expressjs", "url"},
		{"unknown", ""},
	}

	for _, tt := range tests {
		t.Run(tt.language, func(t *testing.T) {
			got := mapLanguageToType(tt.language)
			if got != tt.wantType {
				t.Fatalf("mapLanguageToType(%q) = %q, want %q", tt.language, got, tt.wantType)
			}
		})
	}
}

func TestNormalizeLanguage(t *testing.T) {
	tests := []struct {
		in   string
		want string
		ok   bool
	}{
		{"go", "go", true},
		{"golang", "go", true},
		{"next", "nextjs", true},
		{"solidjs", "solid", true},
		{"expressjs", "express", true},
		{"unknown", "", false},
	}
	for _, tt := range tests {
		got, ok := normalizeLanguage(tt.in)
		if got != tt.want || ok != tt.ok {
			t.Fatalf("normalizeLanguage(%q) = (%q,%v), want (%q,%v)", tt.in, got, ok, tt.want, tt.ok)
		}
	}
}
