package app

import (
	"strings"
	"testing"
)

// Toggling a segment hides it from the rendered status bar; toggling again restores it.
// Reuses inSessionModel (adaptive_layout_test.go): stage "write_script", a long model id.
func TestStatusBarToggleHidesSegment(t *testing.T) {
	t.Setenv("CORREX_CONFIG_HOME", t.TempDir()) // isolate persistence
	m := inSessionModel(220, 30)
	m.sbHidden = map[string]bool{} // start from a clean prefs state

	if !strings.Contains(m.renderStatus(), "write_script") {
		t.Fatal("stage should be visible by default")
	}
	m.toggleStatusSegment("stage")
	if strings.Contains(m.renderStatus(), "write_script") {
		t.Fatal("stage should be hidden after toggle")
	}
	if !strings.Contains(m.renderStatus(), "llama-cpp") {
		t.Fatal("model should still be visible (only stage was hidden)")
	}
	m.toggleStatusSegment("stage")
	if !strings.Contains(m.renderStatus(), "write_script") {
		t.Fatal("stage should be visible again after second toggle")
	}
}

func TestStatusBarModelHide(t *testing.T) {
	t.Setenv("CORREX_CONFIG_HOME", t.TempDir())
	m := inSessionModel(220, 30)
	m.sbHidden = map[string]bool{}
	m.toggleStatusSegment("model")
	if strings.Contains(m.renderStatus(), "llama-cpp") {
		t.Fatal("model should be hidden after toggle")
	}
}

// A toggle persists to tui-prefs.json and is reloaded on the next launch.
func TestStatusBarPrefsPersist(t *testing.T) {
	t.Setenv("CORREX_CONFIG_HOME", t.TempDir())
	m := inSessionModel(220, 30)
	m.sbHidden = map[string]bool{}

	m.toggleStatusSegment("gauge")
	m.toggleStatusSegment("clock")

	reloaded := loadStatusbarHidden()
	if !reloaded["gauge"] || !reloaded["clock"] {
		t.Fatalf("persisted hidden set = %v, want gauge+clock hidden", reloaded)
	}
	if reloaded["stage"] {
		t.Fatal("stage was never toggled; should not be persisted as hidden")
	}
}
