package notification

import (
	"context"
	"encoding/json"
	"net/http"
	"net/http/httptest"
	"path/filepath"
	"testing"
	"time"

	"gitea.dcglab.co.uk/steve/restic-manager/internal/crypto"
	"gitea.dcglab.co.uk/steve/restic-manager/internal/store"
)

func setupHub(t *testing.T) (*Hub, *store.Store) {
	t.Helper()
	dir := t.TempDir()
	st, err := store.Open(context.Background(), filepath.Join(dir, "rm.db"))
	if err != nil {
		t.Fatalf("store: %v", err)
	}
	t.Cleanup(func() { _ = st.Close() })
	keyPath := filepath.Join(dir, "secret.key")
	_ = crypto.GenerateKeyFile(keyPath)
	key, _ := crypto.LoadKeyFromFile(keyPath)
	aead, _ := crypto.NewAEAD(key)
	return NewHub(st, aead, "https://rm.example"), st
}

func TestHubDispatchRecordsLogEntries(t *testing.T) {
	t.Parallel()
	hub, st := setupHub(t)

	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
		w.WriteHeader(200)
	}))
	defer srv.Close()

	cfg, _ := json.Marshal(WebhookConfig{URL: srv.URL})
	enc, err := hub.aead.Encrypt(cfg, []byte("notification-channel:test-ch"))
	if err != nil {
		t.Fatalf("encrypt: %v", err)
	}
	if err := st.CreateNotificationChannel(context.Background(), store.NotificationChannel{
		ID: "test-ch", Kind: "webhook", Name: "test", Enabled: true,
		Config: []byte(enc), CreatedAt: time.Now().UTC(), UpdatedAt: time.Now().UTC(),
	}); err != nil {
		t.Fatalf("create channel: %v", err)
	}

	hub.Dispatch(context.Background(), Payload{
		Event:    EventRaised,
		Severity: "warning",
		Kind:     "backup_failed",
		HostName: "alfa-01",
		Message:  "x",
		RaisedAt: time.Now().UTC(),
	})

	// Verify a log row landed with ok=1.
	var n int
	if err := st.DB().QueryRow(
		`SELECT COUNT(*) FROM notification_log WHERE channel_id = ? AND ok = 1`, "test-ch",
	).Scan(&n); err != nil {
		t.Fatalf("count: %v", err)
	}
	if n != 1 {
		t.Fatalf("expected 1 log row, got %d", n)
	}
}

func TestHubSkipsDisabledChannels(t *testing.T) {
	t.Parallel()
	hub, st := setupHub(t)

	cfg, _ := json.Marshal(WebhookConfig{URL: "http://no-such-host.invalid"})
	enc, _ := hub.aead.Encrypt(cfg, []byte("notification-channel:dis"))
	_ = st.CreateNotificationChannel(context.Background(), store.NotificationChannel{
		ID: "dis", Kind: "webhook", Name: "off", Enabled: false,
		Config: []byte(enc), CreatedAt: time.Now().UTC(), UpdatedAt: time.Now().UTC(),
	})

	hub.Dispatch(context.Background(), Payload{
		Event:    EventRaised,
		AlertID:  "x",
		Severity: "warning",
		Kind:     "backup_failed",
		HostName: "h",
		Message:  "m",
		RaisedAt: time.Now().UTC(),
	})

	var n int
	_ = st.DB().QueryRow(`SELECT COUNT(*) FROM notification_log`).Scan(&n)
	if n != 0 {
		t.Errorf("disabled channel produced log rows: %d", n)
	}
}
