package connector_callout

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

// TestNoVendorNamesInPackage walks all non-test Go source files in this package
// and fails if any vendor-specific names appear. This enforces AC5: the
// connector_callout package must remain vendor-neutral so that any L1 connector
// pack can use it without coupling the L0 substrate to a specific vendor.
func TestNoVendorNamesInPackage(t *testing.T) {
	// Pattern covers common connector-vendor names. Extend when new vendors are
	// on-boarded — the test must reject any vendor name reaching L0 source.
	vendorPattern := regexp.MustCompile(`(?i)\b(hubspot|linear|slack|stripe)\b`)

	pkgDir := "." // package directory (test runs from here)

	entries, err := os.ReadDir(pkgDir)
	if err != nil {
		t.Fatalf("ReadDir: %v", err)
	}

	var hits []string
	for _, e := range entries {
		if e.IsDir() {
			continue
		}
		name := e.Name()
		if !strings.HasSuffix(name, ".go") {
			continue
		}
		// Skip test files.
		if strings.HasSuffix(name, "_test.go") {
			continue
		}
		path := filepath.Join(pkgDir, name)
		content, err := os.ReadFile(path)
		if err != nil {
			t.Errorf("read %s: %v", path, err)
			continue
		}
		if vendorPattern.Match(content) {
			locs := vendorPattern.FindAllIndex(content, -1)
			for _, loc := range locs {
				// Include surrounding context (up to 80 bytes).
				start := loc[0]
				end := loc[1]
				ctxStart := start - 40
				if ctxStart < 0 {
					ctxStart = 0
				}
				ctxEnd := end + 40
				if ctxEnd > len(content) {
					ctxEnd = len(content)
				}
				hits = append(hits, path+": ..."+string(content[ctxStart:ctxEnd])+"...")
			}
		}
	}
	if len(hits) > 0 {
		t.Errorf("vendor names found in %d location(s) — connector_callout must be vendor-neutral:\n%s",
			len(hits), strings.Join(hits, "\n"))
	}
}
