package notifications

import (
	"strings"
	"testing"

	"switchboard-core/models"
)

func TestRenderHTML_Basic(t *testing.T) {
	n := &models.Notification{
		Title: "KB Ready",
		Body:  "Your knowledge base has been processed.",
		Type:  "kb.ready",
	}
	html := RenderHTML(n, "TestInstance")
	if !strings.Contains(html, "KB Ready") {
		t.Error("expected title in HTML output")
	}
	if !strings.Contains(html, "TestInstance") {
		t.Error("expected instance name in HTML output")
	}
	if !strings.Contains(html, "knowledge base") {
		t.Error("expected body in HTML output")
	}
}

func TestRenderHTML_DefaultInstanceName(t *testing.T) {
	n := &models.Notification{Title: "Test"}
	html := RenderHTML(n, "")
	if !strings.Contains(html, "Switchboard Core") {
		t.Error("expected default instance name")
	}
}

func TestRenderText_Basic(t *testing.T) {
	n := &models.Notification{
		Title: "Model Fallback",
		Body:  "GPT-4 fell back to GPT-3.5.",
		Type:  "role.fallback",
	}
	text := RenderText(n, "MyInstance")
	if !strings.Contains(text, "Model Fallback") {
		t.Error("expected title in text output")
	}
	if !strings.Contains(text, "GPT-4 fell back") {
		t.Error("expected body in text output")
	}
}

func TestSubjectForNotification(t *testing.T) {
	tests := []struct {
		notifType string
		title     string
		want      string
	}{
		{"kb.ready", "KB Processed", "[Test] Knowledge Base: KB Processed"},
		{"role.fallback", "Fallback", "[Test] Model Alert: Fallback"},
		{"grant.changed", "Added to team", "[Test] Access Change: Added to team"},
		{"custom.type", "Hello", "[Test] Hello"},
	}
	for _, tt := range tests {
		n := &models.Notification{Type: tt.notifType, Title: tt.title}
		got := SubjectForNotification(n, "Test")
		if got != tt.want {
			t.Errorf("SubjectForNotification(%q, %q) = %q, want %q", tt.notifType, tt.title, got, tt.want)
		}
	}
}
