package notification

import (
	"fmt"
	"testing"
	"time"

	"github.com/prometheus/alertmanager/template"
)

func TestReplaceHtmlLink(t *testing.T) {
	tests := []struct {
		name     string
		input    string
		expected string
	}{
		{
			name:     "test1",
			input:    `url prefix messages <a href="https://testurl.com">Display Links</a> blabla`,
			expected: "url prefix messages <https://testurl.com|Display Links> blabla",
		},
		{
			name:     "test2",
			input:    "url prefix messages <a href=\"https://testurl.com\">Display Links</a> blabla <a href=\"https://testurl23.com\">URL2</a>",
			expected: "url prefix messages <https://testurl.com|Display Links> blabla <https://testurl23.com|URL2>",
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			actual := replaceHtmlLink(tt.input)
			if actual != tt.expected {
				t.Errorf("replaceHtmlLink(%s) = %s; expected %s", tt.input, actual, tt.expected)
			}
		})
	}
}

func TestReplaceWebhookPayload(t *testing.T) {
	alert := template.Alert{
		Status: "firing",
		Labels: map[string]string{
			AlertLabelSeverity: "Info",
		},
		Annotations: map[string]string{
			AlertAnnotationSubject: "Welcome to Alert Subscription on TiDB Cloud",
			AlertAnnotationContent: `
	Welcome to the alert subscription feature on TiDB Cloud. This is a test alert email to confirm that we can successfully send alerts to your email address.<br>
	
	Please confirm that you wish to subscribe to alerts for the [TiDBProjectName] project. This email address will receive notifications whenever an alert is triggered within this project.<br></br>
	
	If everything looks correct, please remember to save the current alert subscriber setup on the page.
	
	If you did not request this test or have any concerns, please contact our <a href="https://docs.pingcap.com/tidbcloud/tidb-cloud-support">support team</a>.
	`,
		},
		StartsAt: time.Now(),
	}
	payload := replaceWebhookPayload(
		ZoomPayload,
		alert,
		"testProject",
		"testCluster",
	)
	fmt.Printf("payload: %s\n", payload)
}
