package mcpdocs

import (
	"strings"
	"testing"
)

const testMCPServerPageHTML = `
<!DOCTYPE html>
<html>
<head>
  <title>GitHub MCP Server | Docker Hub</title>
  <meta name="description" content="MCP server for GitHub automation.">
  <meta property="og:image" content="https://hub.docker.com/image.png">
</head>
<body>
  <span class="category">developer-tools</span>
  <main>
    <h1>GitHub MCP Server</h1>

    <h2>Tools</h2>
    <ul>
      <li><code>search_repos</code><p>Search repositories.</p></li>
      <li><code>open_pr</code><p>Create pull requests.</p></li>
    </ul>

    <h2>Resources</h2>
    <ul>
      <li><code>repo_readme</code><p>Repository readme content.</p></li>
    </ul>

    <h2>Prompts</h2>
    <ul>
      <li><code>summarize_pr</code><p>Summarize pull requests.</p></li>
    </ul>
  </main>
</body>
</html>
`

func TestParseServerPage(t *testing.T) {
	parser := NewParser()

	server, err := parser.ParseServerPage(testMCPServerPageHTML, "https://hub.docker.com/mcp/server/github")
	if err != nil {
		t.Fatalf("ParseServerPage failed: %v", err)
	}

	if server.Name != "GitHub MCP Server" {
		t.Fatalf("unexpected server name: %q", server.Name)
	}
	if server.Description == "" {
		t.Fatal("expected non-empty description")
	}
	if strings.TrimSpace(server.Category) != "developer-tools" {
		t.Fatalf("unexpected category: %q", server.Category)
	}
	if len(server.Tools) != 2 {
		t.Fatalf("expected 2 tools, got %d", len(server.Tools))
	}
	if len(server.Resources) != 1 {
		t.Fatalf("expected 1 resource, got %d", len(server.Resources))
	}
	if len(server.Prompts) != 1 {
		t.Fatalf("expected 1 prompt, got %d", len(server.Prompts))
	}
}

func TestParseHubPage(t *testing.T) {
	parser := NewParser()

	html := `
<div>
  <a href="/mcp/server/github">
    <h3>GitHub MCP Server</h3>
    <p>MCP server for GitHub automation.</p>
  </a>
  <a href="/mcp/server/slack">
    <h3>Slack MCP Server</h3>
    <p>MCP server for Slack workflows.</p>
  </a>
</div>`

	servers, err := parser.ParseHubPage(html)
	if err != nil {
		t.Fatalf("ParseHubPage failed: %v", err)
	}

	if len(servers) != 2 {
		t.Fatalf("expected 2 servers, got %d", len(servers))
	}
	if servers[0].DocURL != "https://hub.docker.com/mcp/server/github" {
		t.Fatalf("unexpected resolved URL: %q", servers[0].DocURL)
	}
}

func TestResolveURL(t *testing.T) {
	got := resolveURL("https://hub.docker.com", "/mcp/server/github")
	if got != "https://hub.docker.com/mcp/server/github" {
		t.Fatalf("resolveURL returned %q", got)
	}
}
