package controlplane

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

func readControlPlaneSourceForStructure(t *testing.T, root string, name string) string {
	t.Helper()
	path := filepath.Join(root, name)
	data, err := os.ReadFile(path)
	if err != nil {
		t.Fatalf("read %s: %v", path, err)
	}
	return string(data)
}

func TestMultiPicklistWrapperRepairStaysOutOfStorageBackfillFile(t *testing.T) {
	root := "."
	storageSrc := readControlPlaneSourceForStructure(t, root, "multipicklist_storage_backfill.go")
	wrapperSrc := readControlPlaneSourceForStructure(t, root, "multipicklist_wrapper_repair.go")

	storageSymbols := []string{
		"func MigrateMultiPicklistStorageBackfill(",
		"func backfillMultiPicklistStorage(",
		"func backfillMultiPicklistColumn(",
		"func fastBackfillSimpleMultiPicklistTable(",
		"func bulkUpdateMultiPicklistBackfillBatch(",
	}
	for _, symbol := range storageSymbols {
		if !strings.Contains(storageSrc, symbol) {
			t.Fatalf("%s should remain in multipicklist_storage_backfill.go", symbol)
		}
		if strings.Contains(wrapperSrc, symbol) {
			t.Fatalf("%s should not move into multipicklist_wrapper_repair.go", symbol)
		}
	}

	wrapperSymbols := []string{
		"const migrationIdMultiPicklistWrapperRepair",
		"func MigrateMultiPicklistWrapperRepair(",
		"type multiPicklistWrapperRepairResult struct",
		"func repairMultiPicklistWrapperStorage(",
		"func decodePicklistOptions(",
		"func repairMultiPicklistWrapperColumn(",
		"func multiPicklistWrapperCandidateSelectQuery(",
		"func jsonStorageSemanticallyEqual(",
	}
	for _, symbol := range wrapperSymbols {
		if strings.Contains(storageSrc, symbol) {
			t.Fatalf("%s should stay in multipicklist_wrapper_repair.go, not multipicklist_storage_backfill.go", symbol)
		}
		if !strings.Contains(wrapperSrc, symbol) {
			t.Fatalf("%s should live in multipicklist_wrapper_repair.go", symbol)
		}
	}
}
