package pipeline

import (
	"testing"

	"textmachine/backend/internal/store"
)

// TestBaseEnrichedMemoryVersionSplit pins the WS1 §1в per-wave bank-version mechanism: the BASE
// version (draft wave / snapshot_W1) excludes Source:"mined" rows, so adding a mined approved row
// after W1 moves ONLY the ENRICHED version (edit wave / snapshot_W2) — keeping W1 checkpoints valid
// («переоплата ОДНА»). A broken split (base folding mined) would move snapshot_W1 and re-bill W1.
func TestBaseEnrichedMemoryVersionSplit(t *testing.T) {
	seedRows := []store.GlossaryEntry{
		{Src: "方源", Dst: "Фан Юань", Status: "approved", Source: "seed"},
		{Src: "古月", Dst: "Гу Юэ", Status: "approved", Source: "ruby"},
	}
	base0 := materializeMemory(seedRows, false)
	// Same rows PLUS a mined approved row (as a W1.5 pass would add).
	enrichedRows := append([]store.GlossaryEntry{}, seedRows...)
	enrichedRows = append(enrichedRows, store.GlossaryEntry{Src: "蛊", Dst: "гу", Status: "approved", Source: "mined"})
	bank1 := materializeMemory(enrichedRows, false)

	// 1) Base version is UNCHANGED by the mined addition (draft wave stays valid).
	if base0.BaseVersion() != bank1.BaseVersion() {
		t.Fatalf("base version moved on a mined-row addition — snapshot_W1 would re-bill W1:\n before %s\n after  %s",
			base0.BaseVersion(), bank1.BaseVersion())
	}
	// 2) Enriched version DID move (edit wave sees the new mined canon).
	if base0.Version() == bank1.Version() {
		t.Fatalf("enriched version did NOT move on a mined-row addition — the edit wave would miss the mined canon")
	}
	// 3) Base ≠ enriched even before any mined row (domain-separated), so a W1 job can never
	//    accidentally content-address to a W2 checkpoint.
	if base0.BaseVersion() == base0.Version() {
		t.Fatalf("base and enriched versions collide over identical rows — they must be domain-separated")
	}
}
