package markdownrenderer

import (
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

var testMarkdown = `# 🔥 FIRING: High CPU Usage on api-gateway

https://signoz.example.com/alerts/123
https://runbooks.example.com/cpu-high

## Alert Details

**Status:** **FIRING** | *api-gateway* service is experiencing high CPU usage. ~~resolved~~ previously.

Alert triggered because ` + "`cpu_usage_percent`" + ` exceeded threshold ` + "`90`" + `.

[View Alert in SigNoz](https://signoz.example.com/alerts/123) | [View Logs](https://signoz.example.com/logs?service=api-gateway) | [View Traces](https://signoz.example.com/traces?service=api-gateway)

![critical](https://signoz.example.com/badges/critical.svg "Critical Alert")

## Alert Labels

| Label    | Value       |
| -------- | ----------- |
| service  | api-gateway |
| instance | pod-5a8b3c  |
| severity | critical    |
| region   | us-east-1   |

## Remediation Steps

1. Check current CPU usage on the pod
2. Review recent deployments for regressions
3. Scale horizontally if load-related
    1. Increase replica count
    2. Verify HPA configuration

## Affected Services

* api-gateway
* auth-service
* payment-service
    * payment-processor
    * payment-validator

## Incident Checklist

- [x] Alert acknowledged
- [x] On-call notified
- [ ] Root cause identified
- [ ] Fix deployed

## Alert Rule Description

> This alert fires when CPU usage exceeds 90% for more than 5 minutes on any pod in the api-gateway service.
>
>> For capacity planning guidelines, see the infrastructure runbook section on horizontal pod autoscaling.

## Triggered Query
` + "```promql\navg(rate(container_cpu_usage_seconds_total{service=\"api-gateway\"}[5m])) by (pod) > 0.9\n```" + `

## Inline Details

This alert was generated by SigNoz using ` + "`alertmanager`" + ` rules engine.
`

func TestRenderHTML_Composite(t *testing.T) {
	html, err := RenderHTML(testMarkdown)
	require.NoError(t, err)

	expected := "<h1>🔥 FIRING: High CPU Usage on api-gateway</h1>\n" +
		"<p><a href=\"https://signoz.example.com/alerts/123\">https://signoz.example.com/alerts/123</a>\n<a href=\"https://runbooks.example.com/cpu-high\">https://runbooks.example.com/cpu-high</a></p>\n" +
		"<h2>Alert Details</h2>\n" +
		"<p><strong>Status:</strong> <strong>FIRING</strong> | <em>api-gateway</em> service is experiencing high CPU usage. <del>resolved</del> previously.</p>\n" +
		"<p>Alert triggered because <code>cpu_usage_percent</code> exceeded threshold <code>90</code>.</p>\n" +
		"<p><a href=\"https://signoz.example.com/alerts/123\">View Alert in SigNoz</a> | <a href=\"https://signoz.example.com/logs?service=api-gateway\">View Logs</a> | <a href=\"https://signoz.example.com/traces?service=api-gateway\">View Traces</a></p>\n" +
		"<p><img src=\"https://signoz.example.com/badges/critical.svg\" alt=\"critical\" title=\"Critical Alert\"></p>\n" +
		"<h2>Alert Labels</h2>\n" +
		"<table>\n<thead>\n<tr>\n<th>Label</th>\n<th>Value</th>\n</tr>\n</thead>\n" +
		"<tbody>\n<tr>\n<td>service</td>\n<td>api-gateway</td>\n</tr>\n" +
		"<tr>\n<td>instance</td>\n<td>pod-5a8b3c</td>\n</tr>\n" +
		"<tr>\n<td>severity</td>\n<td>critical</td>\n</tr>\n" +
		"<tr>\n<td>region</td>\n<td>us-east-1</td>\n</tr>\n</tbody>\n</table>\n" +
		"<h2>Remediation Steps</h2>\n" +
		"<ol>\n<li>Check current CPU usage on the pod</li>\n<li>Review recent deployments for regressions</li>\n<li>Scale horizontally if load-related\n" +
		"<ol>\n<li>Increase replica count</li>\n<li>Verify HPA configuration</li>\n</ol>\n</li>\n</ol>\n" +
		"<h2>Affected Services</h2>\n" +
		"<ul>\n<li>api-gateway</li>\n<li>auth-service</li>\n<li>payment-service\n" +
		"<ul>\n<li>payment-processor</li>\n<li>payment-validator</li>\n</ul>\n</li>\n</ul>\n" +
		"<h2>Incident Checklist</h2>\n" +
		"<ul>\n<li><input checked=\"\" disabled=\"\" type=\"checkbox\"> Alert acknowledged</li>\n" +
		"<li><input checked=\"\" disabled=\"\" type=\"checkbox\"> On-call notified</li>\n" +
		"<li><input disabled=\"\" type=\"checkbox\"> Root cause identified</li>\n" +
		"<li><input disabled=\"\" type=\"checkbox\"> Fix deployed</li>\n</ul>\n" +
		"<h2>Alert Rule Description</h2>\n" +
		"<blockquote>\n<p>This alert fires when CPU usage exceeds 90% for more than 5 minutes on any pod in the api-gateway service.</p>\n" +
		"<blockquote>\n<p>For capacity planning guidelines, see the infrastructure runbook section on horizontal pod autoscaling.</p>\n</blockquote>\n</blockquote>\n" +
		"<h2>Triggered Query</h2>\n" +
		"<pre><code class=\"language-promql\">avg(rate(container_cpu_usage_seconds_total{service=&quot;api-gateway&quot;}[5m])) by (pod) &gt; 0.9\n</code></pre>\n" +
		"<h2>Inline Details</h2>\n" +
		"<p>This alert was generated by SigNoz using <code>alertmanager</code> rules engine.</p>\n"

	assert.Equal(t, expected, html)
}

func TestRenderHTML_InlineFormatting(t *testing.T) {
	input := `# 🔥 FIRING: High CPU on api-gateway
## Alert Status

**FIRING** alert for *api-gateway* service — ~~resolved~~ previously.

Metric ` + "`cpu_usage_percent`" + ` exceeded threshold. [View in SigNoz](https://signoz.example.com/alerts/123)

![critical](https://signoz.example.com/badges/critical.svg "Critical Alert")`

	html, err := RenderHTML(input)
	require.NoError(t, err)

	expected := "<h1>🔥 FIRING: High CPU on api-gateway</h1>\n<h2>Alert Status</h2>\n" +
		"<p><strong>FIRING</strong> alert for <em>api-gateway</em> service — <del>resolved</del> previously.</p>\n" +
		"<p>Metric <code>cpu_usage_percent</code> exceeded threshold. <a href=\"https://signoz.example.com/alerts/123\">View in SigNoz</a></p>\n" +
		"<p><img src=\"https://signoz.example.com/badges/critical.svg\" alt=\"critical\" title=\"Critical Alert\"></p>\n"

	assert.Equal(t, expected, html)
}

func TestRenderHTML_BlockElements(t *testing.T) {
	input := `1. Check CPU usage on the pod
2. Review recent deployments
3. Scale horizontally if needed

* api-gateway
* auth-service
* payment-service

- [x] Alert acknowledged
- [ ] Root cause identified

> This alert fires when CPU usage exceeds 90% for more than 5 minutes.

| Label    | Value       |
| -------- | ----------- |
| service  | api-gateway |
| severity | <no value>    |

` + "```promql\navg(rate(container_cpu_usage_seconds_total{service=\"api-gateway\"}[5m])) by (pod) > 0.9\n```"

	html, err := RenderHTML(input)
	require.NoError(t, err)

	expected := "<ol>\n<li>Check CPU usage on the pod</li>\n<li>Review recent deployments</li>\n<li>Scale horizontally if needed</li>\n</ol>\n" +
		"<ul>\n<li>api-gateway</li>\n<li>auth-service</li>\n<li>payment-service</li>\n</ul>\n" +
		"<ul>\n<li><input checked=\"\" disabled=\"\" type=\"checkbox\"> Alert acknowledged</li>\n" +
		"<li><input disabled=\"\" type=\"checkbox\"> Root cause identified</li>\n</ul>\n" +
		"<blockquote>\n<p>This alert fires when CPU usage exceeds 90% for more than 5 minutes.</p>\n</blockquote>\n" +
		"<table>\n<thead>\n<tr>\n<th>Label</th>\n<th>Value</th>\n</tr>\n</thead>\n" +
		"<tbody>\n<tr>\n<td>service</td>\n<td>api-gateway</td>\n</tr>\n" +
		"<tr>\n<td>severity</td>\n<td>&lt;no value&gt;</td>\n</tr>\n</tbody>\n</table>\n" +
		"<pre><code class=\"language-promql\">avg(rate(container_cpu_usage_seconds_total{service=&quot;api-gateway&quot;}[5m])) by (pod) &gt; 0.9\n</code></pre>\n"

	assert.Equal(t, expected, html)
}
