package feishubot

// Input and output JSON Schema builders for the Feishu Custom Bot catalog,
// mirroring the ported OpenConnector action contracts.

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

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

func nonEmptyStringSchema(description string) map[string]any {
	return map[string]any{
		"type":        "string",
		"minLength":   1,
		"description": description,
	}
}

func rawObjectSchema(description string) map[string]any {
	return map[string]any{
		"$schema":              schemaDialect,
		"type":                 "object",
		"description":          description,
		"additionalProperties": true,
	}
}

func richTextParagraphSchema() map[string]any {
	return map[string]any{
		"type":        "array",
		"minItems":    1,
		"description": "One paragraph of Feishu rich-text tag objects.",
		"items":       rawObjectSchema("One Feishu rich-text tag object."),
	}
}

func richTextLocaleSchema() map[string]any {
	schema := objectSchema(map[string]any{
		"title":   map[string]any{"type": "string", "description": "The rich-text title."},
		"content": richTextParagraphsSchema(),
	}, []string{"content"})
	schema["description"] = "One language block inside the Feishu post payload."
	schema["additionalProperties"] = true
	return schema
}

func richTextParagraphsSchema() map[string]any {
	return map[string]any{
		"type":        "array",
		"minItems":    1,
		"description": "The rich-text paragraphs grouped by line.",
		"items":       richTextParagraphSchema(),
	}
}

func postPayloadSchema() map[string]any {
	locale := richTextLocaleSchema()
	base := objectSchema(map[string]any{
		"zh_cn": locale,
		"en_us": locale,
	}, nil)
	base["description"] = "The Feishu post payload sent as content.post. Include at least one of zh_cn or en_us."
	base["anyOf"] = []any{
		map[string]any{"required": []any{"zh_cn"}},
		map[string]any{"required": []any{"en_us"}},
	}
	return base
}

func sendResultSchema() map[string]any {
	return objectSchema(map[string]any{
		"code":          map[string]any{"type": "integer", "description": "The Feishu response code. 0 means success."},
		"msg":           map[string]any{"type": "string", "description": "The Feishu response message."},
		"data":          rawObjectSchema("The raw Feishu response data object."),
		"statusCode":    map[string]any{"type": "integer", "description": "The legacy response code returned for backward compatibility."},
		"statusMessage": map[string]any{"type": "string", "description": "The legacy response message returned for backward compatibility."},
	}, []string{"code", "msg", "data"})
}

func sendTextInputSchema() map[string]any {
	return objectSchema(map[string]any{
		"text": nonEmptyStringSchema(
			"The text message content. You can include Feishu <at ...> tags inline."),
	}, []string{"text"})
}

func sendPostInputSchema() map[string]any {
	return objectSchema(map[string]any{
		"post": postPayloadSchema(),
	}, []string{"post"})
}

func sendImageInputSchema() map[string]any {
	return objectSchema(map[string]any{
		"imageKey": nonEmptyStringSchema(
			"The Feishu image_key obtained from the image upload API."),
	}, []string{"imageKey"})
}

func sendShareChatInputSchema() map[string]any {
	return objectSchema(map[string]any{
		"shareChatId": nonEmptyStringSchema(
			"The Feishu chat ID used in the share_chat message payload."),
	}, []string{"shareChatId"})
}

func sendInteractiveInputSchema() map[string]any {
	return objectSchema(map[string]any{
		"card": rawObjectSchema(
			"The Feishu interactive card payload sent as the top-level card object."),
	}, []string{"card"})
}
