// Copyright The kweaver.ai Authors.
//
// Licensed under the Apache License, Version 2.0.
// See the LICENSE file in the project root for details.

// Package interfaces defines kn-logic-property-resolver interfaces
package interfaces

import "context"

// ==================== Request and Response Structures ====================

// ResolveLogicPropertiesRequest Logic property resolution request
type ResolveLogicPropertiesRequest struct {
	// Required fields
	KnID               string                   `json:"kn_id" validate:"required"`
	OtID               string                   `json:"ot_id" validate:"required"`
	Query              string                   `json:"query"`
	InstanceIdentities []map[string]interface{} `json:"_instance_identities" validate:"required"`
	Properties         []string                 `json:"properties" validate:"required"`

	// Optional fields
	AdditionalContext string          `json:"additional_context,omitempty"`
	Options           *ResolveOptions `json:"options,omitempty"`

	// Header fields
	AccountID   string `json:"-" header:"x-account-id"`
	AccountType string `json:"-" header:"x-account-type"`
}

// ResolveOptions Optional configuration
type ResolveOptions struct {
	ReturnDebug     bool `json:"return_debug" default:"false"`
	MaxRepairRounds int  `json:"max_repair_rounds" default:"1"`
	MaxConcurrency  int  `json:"max_concurrency" default:"4"`
}

// ResolveLogicPropertiesResponse Logic property resolution response
type ResolveLogicPropertiesResponse struct {
	Datas []map[string]any `json:"datas"`

	// Debug information (returned only when return_debug=true)
	Debug *ResolveDebugInfo `json:"debug,omitempty"`
}

// ResolveDebugInfo Debug information (improved)
type ResolveDebugInfo struct {
	// All dynamic parameters generated by Agent
	DynamicParams map[string]any `json:"dynamic_params,omitempty"`

	// Agent call info (grouped by property)
	AgentInfo map[string]*AgentInfo `json:"agent_info,omitempty"`

	// Server timestamp
	NowMs int64 `json:"now_ms,omitempty"`

	// Warning messages
	Warnings []string `json:"warnings,omitempty"`

	// Trace ID
	TraceID string `json:"trace_id,omitempty"`
}

// AgentInfo Agent call information
type AgentInfo struct {
	// Property type
	PropertyType string `json:"property_type"` // "metric" or "operator"

	// Agent request parameters (stores Agent request structure directly)
	Request AgentRequestDebugInfo `json:"request,omitempty"`

	// Agent response info
	Response *AgentResponseDebugInfo `json:"response,omitempty"`
}

// AgentRequestDebugInfo Agent request debug info
// Directly stores Agent request structure: MetricDynamicParamsGeneratorReq or OperatorDynamicParamsGeneratorReq
type AgentRequestDebugInfo any

// AgentResponseDebugInfo Agent response debug info
// Directly stores Agent response info
type AgentResponseDebugInfo struct {
	// Agent success response: dynamic parameters
	DynamicParams map[string]any `json:"dynamic_params,omitempty"`

	// Agent failure response: error message (corresponds to _error field)
	Error string `json:"_error,omitempty"`
}

// MissingParamsError Missing parameters error response (improved)
type MissingParamsError struct {
	ErrorCode string `json:"error_code"`
	Message   string `json:"message"`

	// Directly return the raw error message generated by Agent
	ErrorMsg string `json:"error_msg,omitempty"`

	// Debug info (returned only when debug is enabled)
	Debug *ResolveDebugInfo `json:"debug,omitempty"`

	Missing []MissingPropertyParams `json:"missing"`
	TraceID string                  `json:"trace_id"`
}

// MissingPropertyParams Missing parameter info (simplified)
type MissingPropertyParams struct {
	Property string `json:"property"`
	// Directly return the error message generated by Agent, no longer parsing specific parameter info
	ErrorMsg string `json:"error_msg,omitempty"`
}

// ==================== Service Interface ====================

// IKnLogicPropertyResolverService Logic property resolver service interface
type IKnLogicPropertyResolverService interface {
	// ResolveLogicProperties Resolve logic properties
	ResolveLogicProperties(ctx context.Context, req *ResolveLogicPropertiesRequest) (*ResolveLogicPropertiesResponse, error)
}

// ==================== LLM Parameter Generation Related Structures ====================

// LLMPromptInput LLM prompt input
type LLMPromptInput struct {
	LogicProperty      *LogicPropertyDef `json:"logic_property"`
	Query              string            `json:"query"`
	InstanceIdentities []map[string]any  `json:"_instance_identities"`
	AdditionalContext  string            `json:"additional_context,omitempty"`
	NowMs              int64             `json:"now_ms,omitempty"`
	Timezone           string            `json:"timezone,omitempty"`
}

// LLMPromptOutput LLM prompt output
type LLMPromptOutput struct {
	// Success output: { "property_name": { "param1": value1, ... } }
	DynamicParams map[string]any `json:"-"`

	// Missing parameter output: { "_error": "..." }
	Error string `json:"_error,omitempty"`
}
