package pkulaw

const schemaDialect = "https://json-schema.org/draft/2020-12/schema"

func objectSchema(
	description string,
	properties map[string]any,
	required ...string,
) map[string]any {
	schema := map[string]any{
		"$schema":              schemaDialect,
		"type":                 "object",
		"description":          description,
		"additionalProperties": false,
		"properties":           properties,
	}
	if len(required) > 0 {
		values := make([]any, 0, len(required))
		for _, name := range required {
			values = append(values, name)
		}
		schema["required"] = values
	}
	return schema
}

func nonWhitespaceStringSchema(description string) map[string]any {
	return map[string]any{
		"type":        "string",
		"minLength":   1,
		"pattern":     "\\S",
		"description": description,
	}
}

func searchLawArticlesInputSchema() map[string]any {
	return objectSchema(
		"Input for semantically searching authoritative legal provisions.",
		map[string]any{
			"query": nonWhitespaceStringSchema(
				"A natural-language legal question or fact pattern in Chinese.",
			),
		},
		"query",
	)
}

func getLawArticleInputSchema() map[string]any {
	return objectSchema(
		"Input for retrieving one authoritative legal provision.",
		map[string]any{
			"title": nonWhitespaceStringSchema(
				"The Chinese title of the law or regulation.",
			),
			"number": nonWhitespaceStringSchema(
				"The article number written in Chinese, such as 第一千零七十七条.",
			),
		},
		"title",
		"number",
	)
}

func keywordSearchInputSchema(description string) map[string]any {
	schema := objectSchema(
		description,
		map[string]any{
			"title": nonWhitespaceStringSchema(
				"A keyword to match against document titles.",
			),
			"fulltext": nonWhitespaceStringSchema(
				"A keyword or phrase to match against document text.",
			),
		},
	)
	schema["anyOf"] = []any{
		map[string]any{"required": []any{"title"}},
		map[string]any{"required": []any{"fulltext"}},
	}
	return schema
}

func searchCasesInputSchema() map[string]any {
	return objectSchema(
		"Input for semantically searching authoritative judicial cases.",
		map[string]any{
			"text": nonWhitespaceStringSchema(
				"A natural-language fact pattern or legal issue in Chinese.",
			),
		},
		"text",
	)
}

func toolResultOutputSchema() map[string]any {
	return objectSchema(
		"The normalized JSON value returned by the PKULaw MCP tool.",
		map[string]any{
			"result": map[string]any{
				"description": "The opaque JSON value returned by the selected PKULaw legal research operation.",
			},
		},
		"result",
	)
}
