package streamgate_test

import (
	"go/parser"
	"go/token"
	"os"
	"path/filepath"
	"strings"
	"testing"
)

// TestStreamgateProductionImportsStayTransportAgnostic verifies that production
// .go files in packages/go/streamgate do not import apps/*/internal or
// proto/gen/iop paths. This ensures the package remains transport-agnostic
// and owns its typed contract exclusively.
func TestStreamgateProductionImportsStayTransportAgnostic(t *testing.T) {
	pkgRoot := "."

	fset := token.NewFileSet()

	entries, err := os.ReadDir(pkgRoot)
	if err != nil {
		t.Fatalf("failed to read directory: %v", err)
	}

	for _, entry := range entries {
		if entry.IsDir() {
			continue
		}

		name := entry.Name()
		// Skip test files; only check production source.
		if strings.HasSuffix(name, "_test.go") {
			continue
		}

		if !strings.HasSuffix(name, ".go") {
			continue
		}

		filePath := filepath.Join(pkgRoot, name)
		f, err := parser.ParseFile(fset, filePath, nil, parser.ImportsOnly)
		if err != nil {
			t.Fatalf("failed to parse %s: %v", name, err)
		}

		for _, imp := range f.Imports {
			importPath := strings.Trim(imp.Path.Value, "\"")

			// Reject apps/*/internal imports.
			if strings.Contains(importPath, "/apps/") && strings.Contains(importPath, "/internal") {
				t.Errorf("%s imports app-internal path: %s", name, importPath)
			}

			// Reject proto/gen/iop imports to ensure Core owns raw wire contract.
			if strings.Contains(importPath, "proto/gen/iop") {
				t.Errorf("%s imports proto gen path: %s", name, importPath)
			}
		}
	}
}
