package controlplane

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

func TestConnectorProjectionSnapshotsStayOutOfInstalledPacksRoot(t *testing.T) {
	rootSrc := readInstalledPackProjectionSourceForStructureTest(t, "installed_packs.go")
	projectionSrc := readInstalledPackProjectionSourceForStructureTest(t, "installed_pack_connector_projection.go")

	for _, symbol := range []string{
		"func connectorInboundProjectionSnapshotID(",
		"func refreshConnectorInboundProjectionSnapshotsAfterPackActivated(",
		"func listActiveConnectorProjectionOwnersForCompanion(",
		"func activeConnectorProjectionOwnerState(",
		"func RefreshConnectorInboundProjectionSnapshotsForActiveConnectorPacks(",
		"func listConnectorProjectionBacklogCompanionInstallIDs(",
		"type connectorInboundProjectionSnapshotCandidate struct",
		"func listLiveConnectorInboundProjectionSnapshotCandidates(",
		"func refreshConnectorInboundProjectionSnapshotsForOwnerPack(",
		"func softDeleteStaleConnectorInboundProjectionSnapshots(",
	} {
		if strings.Contains(rootSrc, symbol) {
			t.Fatalf("installed_packs.go still owns connector projection symbol %q", symbol)
		}
		if !strings.Contains(projectionSrc, symbol) {
			t.Fatalf("installed_pack_connector_projection.go is missing connector projection symbol %q", symbol)
		}
	}
}

func readInstalledPackProjectionSourceForStructureTest(t *testing.T, filename string) string {
	t.Helper()

	_, currentFile, _, ok := runtime.Caller(0)
	if !ok {
		t.Fatal("locate current test file")
	}
	raw, err := os.ReadFile(filepath.Join(filepath.Dir(currentFile), filename))
	if err != nil {
		t.Fatalf("read %s: %v", filename, err)
	}
	return string(raw)
}
