package configgen

import (
	"strings"
	"testing"

	"gitea.dooplex.hu/admin/felhom-hub/internal/store"
)

// The remote debug-mode toggle (hub v0.42.0) relies on this integration: a customer ConfigJSON carrying
// logging.level=debug must deep-merge OVER the template default into the generated controller.yaml, so the
// controller's isDebug() (Logging.Level=="debug") lights up the /debug menu + verbose log. Verified live on
// the demo box (2026-07-10): its controller.yaml carries `logging.level: info` — the exact merge target this
// pins. Without this leg the hub toggle would be a silent no-op.
func TestGenerate_DebugLevelOverride(t *testing.T) {
	const tmpl = "logging:\n  level: info\n"

	// Override present → the generated YAML carries level: debug, and the info default is gone.
	dbg, err := Generate(tmpl, &store.CustomerConfig{
		CustomerID: "c1", ConfigJSON: `{"logging":{"level":"debug"}}`,
	}, nil)
	if err != nil {
		t.Fatalf("generate (debug): %v", err)
	}
	if !strings.Contains(dbg, "level: debug") {
		t.Fatalf("logging.level=debug override did not reach controller.yaml:\n%s", dbg)
	}
	if strings.Contains(dbg, "level: info") {
		t.Fatalf("the info default should have been overridden by debug:\n%s", dbg)
	}

	// No override → the template default (info) stands, debug absent.
	def, err := Generate(tmpl, &store.CustomerConfig{CustomerID: "c1", ConfigJSON: "{}"}, nil)
	if err != nil {
		t.Fatalf("generate (default): %v", err)
	}
	if !strings.Contains(def, "level: info") || strings.Contains(def, "level: debug") {
		t.Fatalf("no override should leave level: info intact:\n%s", def)
	}
}
