package providers

// ProfileSchema describes the available settings for a provider type.
// Used by the admin UI to render forms and by validation on save.
type ProfileSchema struct {
	// Fields lists the configurable settings in display order.
	Fields []ProfileField `json:"fields"`
}

// ProfileField describes a single setting within a provider profile.
type ProfileField struct {
	Key          string      `json:"key"`                    // settings JSON key
	Label        string      `json:"label"`                  // human-readable name
	Description  string      `json:"description,omitempty"`  // help text
	Type         string      `json:"type"`                   // "bool", "int", "string", "enum", "float"
	Default      interface{} `json:"default,omitempty"`      // default value
	EnumValues   []string    `json:"enum_values,omitempty"`  // valid values when Type="enum"
	Min          *int        `json:"min,omitempty"`          // min for int/float
	Max          *int        `json:"max,omitempty"`          // max for int/float
	DependsOn    string      `json:"depends_on,omitempty"`   // only show when this key is truthy
	ProviderOnly bool        `json:"provider_only,omitempty"` // not overridable at persona level
}

// ── Built-in Profile Schemas ────────────────

// intPtr is a helper for literal pointers in struct init.
func intPtr(v int) *int { return &v }

var profileSchemas = map[string]ProfileSchema{
	"openai": {
		Fields: []ProfileField{
			{
				Key:         "system_prompt_prefix",
				Label:       "System Prompt Prefix",
				Description: "Prepended to every system prompt sent to this provider.",
				Type:        "string",
			},
			{
				Key:         "frequency_penalty",
				Label:       "Frequency Penalty",
				Description: "Penalizes repeated tokens. Range: -2.0 to 2.0.",
				Type:        "float",
			},
			{
				Key:         "presence_penalty",
				Label:       "Presence Penalty",
				Description: "Penalizes tokens already present. Range: -2.0 to 2.0.",
				Type:        "float",
			},
		},
	},

	"anthropic": {
		Fields: []ProfileField{
			{
				Key:         "system_prompt_prefix",
				Label:       "System Prompt Prefix",
				Description: "Prepended to every system prompt sent to this provider.",
				Type:        "string",
			},
			{
				Key:         "extended_thinking",
				Label:       "Extended Thinking",
				Description: "Enable Anthropic extended thinking mode. Sends thinking.type=enabled with budget_tokens.",
				Type:        "bool",
				Default:     false,
			},
			{
				Key:         "thinking_budget",
				Label:       "Thinking Budget (tokens)",
				Description: "Max tokens for the thinking phase. Only used when extended_thinking is enabled.",
				Type:        "int",
				Default:     10000,
				Min:         intPtr(1024),
				Max:         intPtr(128000),
				DependsOn:   "extended_thinking",
			},
		},
	},

	"venice": {
		Fields: []ProfileField{
			{
				Key:         "system_prompt_prefix",
				Label:       "System Prompt Prefix",
				Description: "Prepended to every system prompt. Venice also injects its own character prompt.",
				Type:        "string",
			},
			{
				Key:         "enable_thinking",
				Label:       "Enable Thinking",
				Description: "Send venice_parameters.include_venice_system_prompt=false and enable thinking output.",
				Type:        "bool",
				Default:     false,
			},
			{
				Key:         "enable_web_search",
				Label:       "Enable Web Search",
				Description: "Allow Venice to search the web for context (venice_parameters.enable_web_search).",
				Type:        "bool",
				Default:     false,
			},
			{
				Key:         "include_venice_system_prompt",
				Label:       "Include Venice System Prompt",
				Description: "Include Venice's built-in character system prompt.",
				Type:        "bool",
				Default:     true,
			},
		},
	},

	"openrouter": {
		Fields: []ProfileField{
			{
				Key:         "system_prompt_prefix",
				Label:       "System Prompt Prefix",
				Description: "Prepended to every system prompt sent through OpenRouter.",
				Type:        "string",
			},
			{
				Key:          "route",
				Label:        "Routing Strategy",
				Description:  "How OpenRouter selects the underlying provider for a model.",
				Type:         "enum",
				Default:      "auto",
				EnumValues:   []string{"auto", "fallback"},
				ProviderOnly: true,
			},
			{
				Key:         "require_parameters",
				Label:       "Require Parameters Support",
				Description: "Only route to providers that support the requested parameters (tools, temperature, etc.).",
				Type:        "bool",
				Default:     true,
			},
		},
	},
}

// GetProfileSchema returns the profile schema for a provider type.
// Falls back to the openai schema for unknown types (OpenAI-compatible).
func GetProfileSchema(providerType string) ProfileSchema {
	if s, ok := profileSchemas[providerType]; ok {
		return s
	}
	return profileSchemas["openai"]
}

// RegisterProfileSchema adds or replaces a profile schema for a provider type.
// Used for testing and future plugin support.
func RegisterProfileSchema(providerType string, schema ProfileSchema) {
	profileSchemas[providerType] = schema
}
