package markdown

import (
	"strings"
	"testing"
)

func TestRenderRewritesWikiLinksAndTags(t *testing.T) {
	renderer := NewRenderer()

	result, err := renderer.Render([]byte("# Title\n\nLink to [[Project Plan]] and #alpha #beta.\n"))
	if err != nil {
		t.Fatalf("Render() error = %v", err)
	}

	if !strings.Contains(string(result.HTML), `/docs/project-plan`) {
		t.Fatalf("expected wiki-link rewrite, got %s", result.HTML)
	}

	if len(result.Tags) != 2 || result.Tags[0] != "alpha" || result.Tags[1] != "beta" {
		t.Fatalf("unexpected tags: %#v", result.Tags)
	}

	if strings.Contains(string(result.HTML), "<h1") {
		t.Fatalf("expected first h1 to be removed from body html, got %s", result.HTML)
	}
}

func TestRenderPathFallsBackToCleanedFileName(t *testing.T) {
	renderer := NewRenderer()

	result, err := renderer.RenderPath([]byte("Body only\n"), "guide/api-reference.md")
	if err != nil {
		t.Fatalf("RenderPath() error = %v", err)
	}

	if result.Title != "Api Reference" {
		t.Fatalf("Title = %q, want Api Reference", result.Title)
	}
}

func TestRenderPathUsesFolderNameForIndexFiles(t *testing.T) {
	renderer := NewRenderer()

	result, err := renderer.RenderPath([]byte("Body only\n"), "guide/index.md")
	if err != nil {
		t.Fatalf("RenderPath() error = %v", err)
	}

	if result.Title != "Guide" {
		t.Fatalf("Title = %q, want Guide", result.Title)
	}
}

func TestRenderAllowsRawHTML(t *testing.T) {
	renderer := NewRenderer()

	result, err := renderer.Render([]byte("Before <strong>hi</strong> after\n"))
	if err != nil {
		t.Fatalf("Render() error = %v", err)
	}

	if !strings.Contains(string(result.HTML), "<strong>hi</strong>") {
		t.Fatalf("expected raw HTML to render, got %s", result.HTML)
	}
}
