package config

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

// TestDeployConfigLoads parses the file the box actually runs on.
//
// Every other test in this package builds its own JSON, so a key renamed in one
// place and not the other would go unnoticed until the daemon refused to start.
// This one reads deploy/mavend.json through the same Load the daemon calls, so
// a config change and a code change have to agree here or the suite is red.
//
// The ${VAR} expansions come from a gitignored deploy/telegram.env that is not
// present in CI. An unset var expands to the empty string, which is exactly the
// "not configured" state every block already has to handle, so the parse is
// still meaningful without the secrets.
func TestDeployConfigLoads(t *testing.T) {
	path := filepath.Join("..", "..", "deploy", "mavend.json")
	if _, err := os.Stat(path); err != nil {
		t.Skipf("no deploy config at %s: %v", path, err)
	}
	cfg, err := Load(path)
	if err != nil {
		t.Fatalf("Load(%s): %v", path, err)
	}

	// Spot-check the settings whose absence would be a silent behaviour change
	// rather than a startup error.
	if cfg.Phraser == nil {
		t.Fatal("deploy config has no phraser block")
	}
	if cfg.Phraser.NGpuLayers == 0 {
		t.Error("phraser.n_gpu_layers is 0 — llama-server would run CPU-only, " +
			"because nothing in this package defaults that field")
	}
	if cfg.Voice == nil || !cfg.Voice.Enabled {
		t.Fatal("deploy config does not enable voice")
	}
	if !cfg.Voice.UseLLMRouter() {
		t.Error("deploy config turned the LLM router off")
	}
	if cfg.Voice.RouterThreshold <= 0 {
		t.Error("router threshold did not get its default")
	}

	// The second reach (V-649). Deleting this block is how you turn ntfy off,
	// so its absence has to be loud: sev3-away nudges and away reminders route
	// to ntfy and to nothing else, and a nil sink drops them with no log and no
	// outbox row. The token is a ${VAR} that CI cannot resolve, so this checks
	// the wiring and not the credential.
	if cfg.Ntfy == nil {
		t.Fatal("deploy config has no ntfy block — sev3-away and away reminders " +
			"would have nowhere to land, and would vanish silently rather than fail")
	}
	if cfg.Ntfy.BaseURL == "" || cfg.Ntfy.Topic == "" {
		t.Errorf("ntfy block is incomplete: base_url=%q topic=%q", cfg.Ntfy.BaseURL, cfg.Ntfy.Topic)
	}
}
