package boundary_test

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

// L0-B / B1a anti-regrowth seal.
//
// B1a deleted the dead Salesforce bulk-ingest twin (rest/bulk_ingest.go + rest/bulk_handler.go),
// an unwired REST handler that emitted 24 apierr.SF* wire-skin responses. boundarygate does NOT
// scan backend/ (its l0Modules set excludes github.com/nexuscrm/backend), so without this seal
// that SF wire skin could silently regrow under backend/internal/interfaces/rest.
//
// This is a NO-GROWTH count seal (it must only ever shrink). After B1b neutralized the
// rest/helpers.go projection — the dead *SalesforceAPIError arm, the SFErrorFromDomainForVersion
// TestMode arm, and the RequireParam SF emission — app-blind rest/ carries ZERO apierr.SF* wire
// skin; the active dialect's projector owns all v63 mapping. Any reintroduction fails this seal.
const restAPIErrSFLedger = 0

func TestRESTBulkWireSkinDoesNotRegrow(t *testing.T) {
	root := repoRoot(t)
	dir := filepath.Join(root, "backend", "internal", "interfaces", "rest")

	total := 0
	perFile := map[string]int{}
	for _, file := range productionGoFilesRecursive(t, dir) {
		b, err := os.ReadFile(file)
		if err != nil {
			t.Fatalf("read %s: %v", file, err)
		}
		if n := strings.Count(string(b), "apierr.SF"); n > 0 {
			total += n
			perFile[toRepoRelative(t, root, file)] = n
		}
	}

	switch {
	case total > restAPIErrSFLedger:
		t.Fatalf("apierr.SF* wire skin GREW in backend/internal/interfaces/rest: got %d, ledger %d (per-file: %v).\n"+
			"The dead bulk twin was deleted in B1a — do not reintroduce Salesforce wire skin in app-blind REST handlers.",
			total, restAPIErrSFLedger, perFile)
	case total < restAPIErrSFLedger:
		t.Fatalf("apierr.SF* count in rest/ dropped to %d (ledger %d) — good. Lower restAPIErrSFLedger to %d to keep the seal tight.",
			total, restAPIErrSFLedger, total)
	}
}
