package mcp

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

	"github.com/stretchr/testify/require"
)

func TestProductionIdentifiersUseInitialisms(t *testing.T) {
	for _, directory := range []string{".", "../exec"} {
		entries, err := os.ReadDir(directory)
		require.NoError(t, err)
		for _, entry := range entries {
			if entry.IsDir() || !strings.HasSuffix(entry.Name(), ".go") || strings.HasSuffix(entry.Name(), "_test.go") {
				continue
			}
			path := filepath.Join(directory, entry.Name())
			file, parseErr := parser.ParseFile(token.NewFileSet(), path, nil, 0)
			require.NoError(t, parseErr)
			ast.Inspect(file, func(node ast.Node) bool {
				identifier, ok := node.(*ast.Ident)
				if !ok {
					return true
				}
				for _, forbidden := range []string{"pR", "jSON", "Url", "Http", "Api", "Mcp"} {
					require.NotContainsf(t, identifier.Name, forbidden, "%s: %s", path, identifier.Name)
				}
				return true
			})
		}
	}
}

func TestProductionHasNoEmptyDeclarationBlocks(t *testing.T) {
	for _, directory := range []string{".", "../exec"} {
		entries, err := os.ReadDir(directory)
		require.NoError(t, err)
		for _, entry := range entries {
			if entry.IsDir() || !strings.HasSuffix(entry.Name(), ".go") || strings.HasSuffix(entry.Name(), "_test.go") {
				continue
			}
			path := filepath.Join(directory, entry.Name())
			file, parseErr := parser.ParseFile(token.NewFileSet(), path, nil, 0)
			require.NoError(t, parseErr)
			for _, declaration := range file.Decls {
				group, ok := declaration.(*ast.GenDecl)
				if ok && group.Lparen.IsValid() {
					require.NotEmptyf(t, group.Specs, "%s contains empty %s declaration", path, group.Tok)
				}
			}
		}
	}
}
