package stacks

// SCRATCH (not committed; a copy of controller d48da6c in the scratchpad) — Part C, 2026-09-30:
// what the night leg and the Update button do with an app AT THE HEAD TAG whose catalog carries a
// NEWER tested digest for that same tag (a same-name repush the catalog re-tested).

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

func TestPartC_DigestOnlyBehindAtHead(t *testing.T) {
	m, dir, _, ups, logBuf := legManager(t)
	catDir := filepath.Dir(m.CatalogTemplatePath("nextcloud", "docker-compose.yml"))
	// the ladder: B -> C tested at digest dA, tested 2026-09-29
	mustWrite(t, filepath.Join(catDir, ".felhom.yml"), "display_name: Nextcloud\nupdate_ladder:\n"+
		`  - {"from": {"web": "`+ladderB+`"}, "to": {"web": "`+ladderC+`"}, "digest": {"web": "`+dA+`"}, "verdict": "proven", "tested_at": "2026-09-29T00:00:00Z"}`+"\n")
	// the box: at C (the head tag), running the OLDER digest dB, installed 2026-09-20
	cur := "services:\n  web:\n    image: " + ladderC + "@" + dB + "\nvolumes:\n  db:\n"
	mustWrite(t, ComposePathIn(dir), cur)
	mustWrite(t, AppliedComposePath(dir), cur)
	mustWrite(t, filepath.Join(dir, "app.yaml"), "deployed: true\nenv: {}\npinned_images:\n    web: "+ladderC+
		"\ninstalled_images:\n    web:\n        ref: "+ladderC+"\n        digest: "+dB+"\n        at: \"2026-09-20T00:00:00Z\"\n")
	if err := m.ScanStacks(); err != nil {
		t.Fatal(err)
	}
	st, _ := m.GetStack("nextcloud")
	t.Logf("1. CatalogOrder = %v (catalog digests %v, tested %v; installed %+v)", CatalogOrder(*st), st.CatalogDigests, st.CatalogTestedAt, st.AppConfig.InstalledImages["web"])
	legOpts(m, nil)
	s := mustLeg(t, m)
	t.Logf("2. night leg: presses=%d step=%+v", len(*ups), legStepFor(s, "nextcloud"))
	for _, l := range strings.Split(logBuf.String(), "\n") {
		if strings.Contains(l, "update-leg") {
			t.Logf("   log: %s", l)
		}
	}
	// 3. the household's Update button
	if err := m.StartGuardedUpdate("nextcloud"); err != nil {
		t.Logf("3. Update button refused: %v", err)
		return
	}
	st2 := waitUpdateDone(t, m, "nextcloud")
	body := fileBody(t, ComposePathIn(dir))
	t.Logf("3. Update button: phase=%s err=%q presses=%d; live compose pins the tested digest dA: %v; pin=%s",
		st2.UpdatePhase, st2.UpdateError, len(*ups), strings.Contains(body, ladderC+"@"+dA), pinOf(t, dir))
}

// Option (a)'s box half, measured before it is costed: IF the catalog carried a same-tag re-test
// (from == to == C, tested at the new digest dA, newer than the install), what would the leg do?
func TestPartC_IfTheCatalogWroteARefreshEntry(t *testing.T) {
	m, dir, _, ups, logBuf := legManager(t)
	catDir := filepath.Dir(m.CatalogTemplatePath("nextcloud", "docker-compose.yml"))
	mustWrite(t, filepath.Join(catDir, ".felhom.yml"), "display_name: Nextcloud\nupdate_ladder:\n"+
		`  - {"from": {"web": "`+ladderB+`"}, "to": {"web": "`+ladderC+`"}, "digest": {"web": "`+dB+`"}, "verdict": "proven", "tested_at": "2026-09-19T00:00:00Z"}`+"\n"+
		`  - {"from": {"web": "`+ladderC+`"}, "to": {"web": "`+ladderC+`"}, "digest": {"web": "`+dA+`"}, "verdict": "proven", "tested_at": "2026-09-29T00:00:00Z"}`+"\n")
	cur := "services:\n  web:\n    image: " + ladderC + "@" + dB + "\nvolumes:\n  db:\n"
	mustWrite(t, ComposePathIn(dir), cur)
	mustWrite(t, AppliedComposePath(dir), cur)
	mustWrite(t, filepath.Join(dir, "app.yaml"), "deployed: true\nenv: {}\npinned_images:\n    web: "+ladderC+
		"\ninstalled_images:\n    web:\n        ref: "+ladderC+"\n        digest: "+dB+"\n        at: \"2026-09-20T00:00:00Z\"\n")
	if err := m.ScanStacks(); err != nil {
		t.Fatal(err)
	}
	st, _ := m.GetStack("nextcloud")
	t.Logf("1. CatalogOrder = %v", CatalogOrder(*st))
	legOpts(m, nil)
	s := mustLeg(t, m)
	body := fileBody(t, ComposePathIn(dir))
	t.Logf("2. night leg: presses=%d step=%+v; live compose pins dA: %v", len(*ups), legStepFor(s, "nextcloud"), strings.Contains(body, ladderC+"@"+dA))
	for _, l := range strings.Split(logBuf.String(), "\n") {
		if strings.Contains(l, "update-leg") || strings.Contains(l, "step") {
			t.Logf("   log: %s", l)
		}
	}
	s2 := mustLeg(t, m)
	t.Logf("3. the next night: presses=%d step=%+v", len(*ups), legStepFor(s2, "nextcloud"))
}
