package config

import (
	"os"
	"path/filepath"
	"reflect"
	"testing"
	"time"
)

func TestReadConfig(t *testing.T) {
	configContent := `
base-url https://ntfy-alertmanager.example.com
http-address :8080
log-level info
log-format json
log-file /var/log/ntfy-alertmanager.log
alert-mode multi
user webhookUser
password webhookPass

labels {
    order "severity,instance"

    severity "critical" {
        priority 5
        tags "rotating_light"
        icon "https://example.com/critical.png"
        email-address foo@example.com
        call yes
    }

    severity "info" {
        priority 1
    }

    instance "example.com" {
        tags "computer,example"
        topic https://ntfy.sh/homeserver
    }
}

resolved {
    tags "resolved,partying_face"
    icon "https://example.com/resolved.png"
    priority 1
}

ntfy {
    topic https://ntfy.sh/alertmanager-alerts
    certificate-fingerprint 13:6D:2B:88:9C:57:36:D0:81:B4:B2:9C:79:09:27:62:92:CF:B8:6A:6B:D3:AD:46:35:CB:70:17:EB:99:6E:28:08:2A:B8:C6:79:4B:F6:2E:81:79:41:98:1D:53:C8:07:B3:5C:24:5F:B1:8E:B6:FB:66:B5:DD:B4:D0:5C:29:91
    user user
    password pass
    generator-url-label source
}

alertmanager {
    silence-duration 24h
    user user
    password pass
    url https://alertmanager.example.com
}

cache {
    type redis
    duration 48h
    redis-url redis://user:password@localhost:6789/3
}
`

	expectedCfg := &Config{
		BaseURL:     "https://ntfy-alertmanager.example.com",
		HTTPAddress: ":8080",
		LogLevel:    "info",
		LogFormat:   "json",
		LogFile:     "/var/log/ntfy-alertmanager.log",
		AlertMode:   Multi,
		User:        "webhookUser",
		Password:    "webhookPass",
		Ntfy: ntfyConfig{
			Topic:             "https://ntfy.sh/alertmanager-alerts",
			User:              "user",
			Password:          "pass",
			CertFingerprint:   "136d2b889c5736d081b4b29c7909276292cfb86a6bd3ad4635cb7017eb996e28082ab8c6794bf62e817941981d53c807b35c245fb18eb6fb66b5ddb4d05c2991",
			GeneratorURLLabel: "source",
		},
		Labels: labels{Order: []string{"severity", "instance"},
			Label: map[string]labelConfig{
				"severity:critical": {
					Priority:     "5",
					Tags:         []string{"rotating_light"},
					Icon:         "https://example.com/critical.png",
					EmailAddress: "foo@example.com",
					Call:         "yes",
				},
				"severity:info": {Priority: "1"},
				"instance:example.com": {
					Tags:  []string{"computer", "example"},
					Topic: "https://ntfy.sh/homeserver"},
			},
		},
		Cache: CacheConfig{
			Type:            "redis",
			Duration:        48 * time.Hour,
			CleanupInterval: time.Hour,
			RedisURL:        "redis://user:password@localhost:6789/3",
		},
		Am: alertmanagerConfig{
			SilenceDuration: time.Hour * 24,
			User:            "user",
			Password:        "pass",
			URL:             "https://alertmanager.example.com",
		},
		Resolved: resolvedConfig{
			Tags:     []string{"resolved", "partying_face"},
			Icon:     "https://example.com/resolved.png",
			Priority: "1",
		},
	}

	configPath := filepath.Join(t.TempDir(), "config")
	err := os.WriteFile(configPath, []byte(configContent), 0600)
	if err != nil {
		t.Errorf("failed to write config file: %v", err)
	}

	cfg, err := Read(configPath)
	if err != nil {
		t.Errorf("failed to read config file: %v", err)
	}

	if !reflect.DeepEqual(cfg, expectedCfg) {
		t.Errorf("expected: %v, got %v", expectedCfg, cfg)
	}
}