package config

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

func TestLoadReadsFileAndEnvOverrides(t *testing.T) {
	t.Setenv("MORZ_INFOBOARD_CONFIG", filepath.Join(t.TempDir(), "config.json"))
	configPath := os.Getenv("MORZ_INFOBOARD_CONFIG")

	content := []byte(`{
		"screen_id": "file-screen",
		"server_base_url": "http://file.example",
		"mqtt_broker": "tcp://file-broker:1883",
		"heartbeat_every_seconds": 45,
		"status_report_every_seconds": 90
	}`)

	if err := os.WriteFile(configPath, content, 0o644); err != nil {
		t.Fatalf("WriteFile() error = %v", err)
	}

	t.Setenv("MORZ_INFOBOARD_SCREEN_ID", "env-screen")

	cfg, err := Load()
	if err != nil {
		t.Fatalf("Load() error = %v", err)
	}

	if got, want := cfg.ScreenID, "env-screen"; got != want {
		t.Fatalf("ScreenID = %q, want %q", got, want)
	}

	if got, want := cfg.ServerBaseURL, "http://file.example"; got != want {
		t.Fatalf("ServerBaseURL = %q, want %q", got, want)
	}

	if got, want := cfg.HeartbeatEvery, 45; got != want {
		t.Fatalf("HeartbeatEvery = %d, want %d", got, want)
	}

	if got, want := cfg.StatusReportEvery, 90; got != want {
		t.Fatalf("StatusReportEvery = %d, want %d", got, want)
	}
}

func TestLoadFallsBackToDefaultStatusReportInterval(t *testing.T) {
	t.Setenv("MORZ_INFOBOARD_CONFIG", filepath.Join(t.TempDir(), "missing.json"))

	cfg, err := Load()
	if err != nil {
		t.Fatalf("Load() error = %v", err)
	}

	if got, want := cfg.StatusReportEvery, 60; got != want {
		t.Fatalf("StatusReportEvery = %d, want %d", got, want)
	}
}
