package server

import (
	"testing"

	"github.com/CalmoAI/mcp-server-signoz/pkg/mcp"
)

// TestArraySchemaValidation specifically tests that all array parameters
// have proper "items" definitions to prevent Gemini API validation errors
func TestArraySchemaValidation(t *testing.T) {
	// Create a test server using the existing helper
	server := createTestServer()

	for toolName, tool := range server.tools {
		t.Run(toolName, func(t *testing.T) {
			validateToolArrayProperties(t, toolName, tool)
		})
	}
}

// validateToolArrayProperties checks that all array type properties have proper items definitions
func validateToolArrayProperties(t *testing.T, toolName string, tool mcp.Tool) {
	for propName, prop := range tool.InputSchema.Properties {
		if prop.Type == "array" {
			if prop.Items == nil {
				t.Errorf("Tool %s, property %s: array type missing 'items' field (this causes Gemini API validation errors)",
					toolName, propName)
			} else {
				// Validate that items has a proper type
				if prop.Items.Type == "" {
					t.Errorf("Tool %s, property %s: array items missing 'type' field",
						toolName, propName)
				}

				// For our specific case, validate expected array types
				switch propName {
				case "filters":
					if prop.Items.Type != "object" {
						t.Errorf("Tool %s, property %s: expected items type 'object', got '%s'",
							toolName, propName, prop.Items.Type)
					}
				case "group_by":
					if prop.Items.Type != "string" {
						t.Errorf("Tool %s, property %s: expected items type 'string', got '%s'",
							toolName, propName, prop.Items.Type)
					}
				}
			}
		}
	}
}

// TestSpecificGeminiValidationIssue tests the exact issue that was failing with Gemini
func TestSpecificGeminiValidationIssue(t *testing.T) {
	server := createTestServer()

	// Check specifically for signoz_execute_builder_query
	builderTool, exists := server.tools["signoz_execute_builder_query"]
	if !exists {
		t.Fatal("signoz_execute_builder_query tool not found")
	}

	// Check filters property
	filtersProp, exists := builderTool.InputSchema.Properties["filters"]
	if !exists {
		t.Fatal("filters property not found in signoz_execute_builder_query")
	}

	if filtersProp.Type != "array" {
		t.Errorf("filters property should be array type, got %s", filtersProp.Type)
	}

	if filtersProp.Items == nil {
		t.Fatal("filters property missing items definition - this causes Gemini validation error")
	}

	if filtersProp.Items.Type != "object" {
		t.Errorf("filters items should be object type, got %s", filtersProp.Items.Type)
	}

	// Check group_by property
	groupByProp, exists := builderTool.InputSchema.Properties["group_by"]
	if !exists {
		t.Fatal("group_by property not found in signoz_execute_builder_query")
	}

	if groupByProp.Type != "array" {
		t.Errorf("group_by property should be array type, got %s", groupByProp.Type)
	}

	if groupByProp.Items == nil {
		t.Fatal("group_by property missing items definition - this causes Gemini validation error")
	}

	if groupByProp.Items.Type != "string" {
		t.Errorf("group_by items should be string type, got %s", groupByProp.Items.Type)
	}

	t.Logf("✅ Schema validation passed: Both filters and group_by arrays have proper items definitions")
}

// TestJSONSchemaCompliance tests that all tool schemas are valid JSON Schema
func TestJSONSchemaCompliance(t *testing.T) {
	server := createTestServer()

	for toolName, tool := range server.tools {
		t.Run(toolName, func(t *testing.T) {
			// Basic JSON Schema validation
			if tool.InputSchema.Type != "object" {
				t.Errorf("Tool %s: input schema type should be 'object', got '%s'",
					toolName, tool.InputSchema.Type)
			}

			// Ensure all properties have types
			for propName, prop := range tool.InputSchema.Properties {
				if prop.Type == "" {
					t.Errorf("Tool %s, property %s: missing type field", toolName, propName)
				}

				// Validate array types specifically
				if prop.Type == "array" && prop.Items == nil {
					t.Errorf("Tool %s, property %s: array type must have items definition",
						toolName, propName)
				}
			}
		})
	}
}
