package com.garfiec.librechat.core.ui.components

import com.garfiec.librechat.core.model.ParameterDefinition
import com.garfiec.librechat.core.model.ParameterType

/**
 * Registry of parameter definitions per endpoint, matching the official LibreChat web app's
 * parameterSettings.ts. Each endpoint maps to an ordered list of ParameterDefinition that
 * drives the dynamic ModelParameterSheet rendering.
 */
object EndpointParameterRegistry {

    fun getDefinitions(endpoint: String): List<ParameterDefinition> {
        val key = endpoint.lowercase()
        return ENDPOINT_PARAMS[key] ?: ENDPOINT_PARAMS["default"]!!
    }

    private val ENDPOINT_PARAMS: Map<String, List<ParameterDefinition>> = mapOf(
        "openai" to openAiParams(),
        "azureopenai" to openAiParams(),
        "custom" to openAiParams(),
        "anthropic" to anthropicParams(),
        "google" to googleParams(),
        "bedrock" to bedrockParams(),
        "default" to defaultParams(),
    )

    private fun openAiParams() = listOf(
        ParameterDefinition(
            key = "chatGptLabel",
            label = "Custom Name",
            type = ParameterType.TEXT,
            default = "",
            description = "Set a custom name for the AI.",
        ),
        ParameterDefinition(
            key = "promptPrefix",
            label = "Custom Instructions",
            type = ParameterType.TEXTAREA,
            default = "",
            description = "Set custom instructions to include in System Message.",
        ),
        ParameterDefinition(
            key = "maxContextTokens",
            label = "Max Context Tokens",
            type = ParameterType.TEXT,
            default = "",
            description = "Max context tokens for this conversation.",
        ),
        ParameterDefinition(
            key = "max_tokens",
            label = "Max Output Tokens",
            type = ParameterType.TEXT,
            default = "",
            description = "Max tokens in the response.",
        ),
        ParameterDefinition(
            key = "temperature",
            label = "Temperature",
            type = ParameterType.SLIDER,
            min = 0.0,
            max = 2.0,
            step = 0.01,
            default = "1.0",
            description = "Controls randomness. Lower values are more focused and deterministic.",
        ),
        ParameterDefinition(
            key = "top_p",
            label = "Top P",
            type = ParameterType.SLIDER,
            min = 0.0,
            max = 1.0,
            step = 0.01,
            default = "1.0",
            description = "Nucleus sampling. Controls diversity via cumulative probability cutoff.",
        ),
        ParameterDefinition(
            key = "frequency_penalty",
            label = "Frequency Penalty",
            type = ParameterType.SLIDER,
            min = -2.0,
            max = 2.0,
            step = 0.01,
            default = "0.0",
            description = "Penalizes tokens based on their frequency in the text so far.",
        ),
        ParameterDefinition(
            key = "presence_penalty",
            label = "Presence Penalty",
            type = ParameterType.SLIDER,
            min = -2.0,
            max = 2.0,
            step = 0.01,
            default = "0.0",
            description = "Penalizes tokens based on whether they appear in the text so far.",
        ),
        ParameterDefinition(
            key = "stop",
            label = "Stop Sequences",
            type = ParameterType.TAGS,
            max = 4.0,
            default = "",
            description = "Up to 4 sequences where the model will stop generating.",
        ),
        ParameterDefinition(
            key = "resendFiles",
            label = "Resend Files",
            type = ParameterType.SWITCH,
            default = "false",
            description = "Resend previously attached files with every message.",
        ),
        ParameterDefinition(
            key = "reasoning_effort",
            label = "Reasoning Effort",
            type = ParameterType.DROPDOWN,
            options = listOf("", "none", "minimal", "low", "medium", "high", "xhigh"),
            default = "",
            description = "Controls how much reasoning the model performs.",
        ),
        ParameterDefinition(
            key = "web_search",
            label = "Web Search",
            type = ParameterType.SWITCH,
            default = "false",
            description = "Enable web search to get up-to-date information.",
        ),
        ParameterDefinition(
            key = "fileTokenLimit",
            label = "File Token Limit",
            type = ParameterType.TEXT,
            default = "",
            description = "Maximum number of tokens from attached files.",
        ),
    )

    private fun anthropicParams() = listOf(
        ParameterDefinition(
            key = "modelLabel",
            label = "Custom Name",
            type = ParameterType.TEXT,
            default = "",
            description = "Set a custom name for the AI.",
        ),
        ParameterDefinition(
            key = "promptPrefix",
            label = "Custom Instructions",
            type = ParameterType.TEXTAREA,
            default = "",
            description = "Set custom instructions to include in System Message.",
        ),
        ParameterDefinition(
            key = "maxContextTokens",
            label = "Max Context Tokens",
            type = ParameterType.TEXT,
            default = "",
            description = "Max context tokens for this conversation.",
        ),
        ParameterDefinition(
            key = "maxOutputTokens",
            label = "Max Output Tokens",
            type = ParameterType.TEXT,
            min = 1.0,
            max = 128000.0,
            default = "",
            description = "Max tokens in the response (1-128000).",
        ),
        ParameterDefinition(
            key = "temperature",
            label = "Temperature",
            type = ParameterType.SLIDER,
            min = 0.0,
            max = 1.0,
            step = 0.01,
            default = "1.0",
            description = "Controls randomness. Lower values are more focused and deterministic.",
        ),
        ParameterDefinition(
            key = "topP",
            label = "Top P",
            type = ParameterType.SLIDER,
            min = 0.0,
            max = 1.0,
            step = 0.01,
            default = "0.7",
            description = "Nucleus sampling. Controls diversity via cumulative probability cutoff.",
        ),
        ParameterDefinition(
            key = "topK",
            label = "Top K",
            type = ParameterType.SLIDER,
            min = 1.0,
            max = 40.0,
            step = 1.0,
            default = "5",
            description = "Limits token selection to the top K most likely tokens.",
        ),
        ParameterDefinition(
            key = "resendFiles",
            label = "Resend Files",
            type = ParameterType.SWITCH,
            default = "false",
            description = "Resend previously attached files with every message.",
        ),
        ParameterDefinition(
            key = "promptCache",
            label = "Prompt Caching",
            type = ParameterType.SWITCH,
            default = "true",
            description = "Enable prompt caching to reduce latency and cost.",
        ),
        ParameterDefinition(
            key = "thinking",
            label = "Thinking",
            type = ParameterType.SWITCH,
            default = "true",
            description = "Enable extended thinking.",
        ),
        ParameterDefinition(
            key = "thinkingBudget",
            label = "Thinking Budget",
            type = ParameterType.TEXT,
            min = 1024.0,
            max = 200000.0,
            default = "",
            description = "Max tokens for thinking (1024-200000).",
        ),
        ParameterDefinition(
            key = "effort",
            label = "Effort",
            type = ParameterType.DROPDOWN,
            options = listOf("", "low", "medium", "high", "max"),
            default = "",
            description = "Controls the overall effort level of the response.",
        ),
        ParameterDefinition(
            key = "web_search",
            label = "Web Search",
            type = ParameterType.SWITCH,
            default = "false",
            description = "Enable web search to get up-to-date information.",
        ),
        ParameterDefinition(
            key = "fileTokenLimit",
            label = "File Token Limit",
            type = ParameterType.TEXT,
            default = "",
            description = "Maximum number of tokens from attached files.",
        ),
    )

    private fun googleParams() = listOf(
        ParameterDefinition(
            key = "modelLabel",
            label = "Custom Name",
            type = ParameterType.TEXT,
            default = "",
            description = "Set a custom name for the AI.",
        ),
        ParameterDefinition(
            key = "promptPrefix",
            label = "Custom Instructions",
            type = ParameterType.TEXTAREA,
            default = "",
            description = "Set custom instructions to include in System Message.",
        ),
        ParameterDefinition(
            key = "maxContextTokens",
            label = "Max Context Tokens",
            type = ParameterType.TEXT,
            default = "",
            description = "Max context tokens for this conversation.",
        ),
        ParameterDefinition(
            key = "maxOutputTokens",
            label = "Max Output Tokens",
            type = ParameterType.TEXT,
            min = 1.0,
            max = 64000.0,
            default = "",
            description = "Max tokens in the response (1-64000).",
        ),
        ParameterDefinition(
            key = "temperature",
            label = "Temperature",
            type = ParameterType.SLIDER,
            min = 0.0,
            max = 2.0,
            step = 0.01,
            default = "1.0",
            description = "Controls randomness. Lower values are more focused and deterministic.",
        ),
        ParameterDefinition(
            key = "topP",
            label = "Top P",
            type = ParameterType.SLIDER,
            min = 0.0,
            max = 1.0,
            step = 0.01,
            default = "0.95",
            description = "Nucleus sampling. Controls diversity via cumulative probability cutoff.",
        ),
        ParameterDefinition(
            key = "topK",
            label = "Top K",
            type = ParameterType.SLIDER,
            min = 1.0,
            max = 40.0,
            step = 1.0,
            default = "40",
            description = "Limits token selection to the top K most likely tokens.",
        ),
        ParameterDefinition(
            key = "resendFiles",
            label = "Resend Files",
            type = ParameterType.SWITCH,
            default = "false",
            description = "Resend previously attached files with every message.",
        ),
        ParameterDefinition(
            key = "thinking",
            label = "Thinking",
            type = ParameterType.SWITCH,
            default = "true",
            description = "Enable extended thinking.",
        ),
        ParameterDefinition(
            key = "thinkingBudget",
            label = "Thinking Budget",
            type = ParameterType.TEXT,
            min = -1.0,
            max = 32000.0,
            default = "",
            description = "Max tokens for thinking (-1 = dynamic, up to 32000).",
        ),
        ParameterDefinition(
            key = "thinkingLevel",
            label = "Thinking Level",
            type = ParameterType.DROPDOWN,
            options = listOf("", "none", "low", "medium", "high"),
            default = "",
            description = "Controls how much thinking the model performs.",
        ),
        ParameterDefinition(
            key = "web_search",
            label = "Grounding with Google Search",
            type = ParameterType.SWITCH,
            default = "false",
            description = "Enable web search to get up-to-date information.",
        ),
        ParameterDefinition(
            key = "fileTokenLimit",
            label = "File Token Limit",
            type = ParameterType.TEXT,
            default = "",
            description = "Maximum number of tokens from attached files.",
        ),
    )

    private fun bedrockParams() = listOf(
        ParameterDefinition(
            key = "modelLabel",
            label = "Custom Name",
            type = ParameterType.TEXT,
            default = "",
            description = "Set a custom name for the AI.",
        ),
        ParameterDefinition(
            key = "promptPrefix",
            label = "Custom Instructions",
            type = ParameterType.TEXTAREA,
            default = "",
            description = "Set custom instructions to include in System Message.",
        ),
        ParameterDefinition(
            key = "maxContextTokens",
            label = "Max Context Tokens",
            type = ParameterType.TEXT,
            default = "",
            description = "Max context tokens for this conversation.",
        ),
        ParameterDefinition(
            key = "maxTokens",
            label = "Max Output Tokens",
            type = ParameterType.TEXT,
            default = "",
            description = "Max tokens in the response.",
        ),
        ParameterDefinition(
            key = "temperature",
            label = "Temperature",
            type = ParameterType.SLIDER,
            min = 0.0,
            max = 1.0,
            step = 0.01,
            default = "1.0",
            description = "Controls randomness. Lower values are more focused and deterministic.",
        ),
        ParameterDefinition(
            key = "topP",
            label = "Top P",
            type = ParameterType.SLIDER,
            min = 0.0,
            max = 1.0,
            step = 0.01,
            default = "0.7",
            description = "Nucleus sampling. Controls diversity via cumulative probability cutoff.",
        ),
        ParameterDefinition(
            key = "topK",
            label = "Top K",
            type = ParameterType.SLIDER,
            min = 0.0,
            max = 500.0,
            step = 1.0,
            default = "0",
            description = "Limits token selection to the top K most likely tokens.",
        ),
        ParameterDefinition(
            key = "resendFiles",
            label = "Resend Files",
            type = ParameterType.SWITCH,
            default = "false",
            description = "Resend previously attached files with every message.",
        ),
        ParameterDefinition(
            key = "promptCache",
            label = "Prompt Caching",
            type = ParameterType.SWITCH,
            default = "false",
            description = "Enable prompt caching to reduce latency and cost.",
        ),
        ParameterDefinition(
            key = "thinking",
            label = "Thinking",
            type = ParameterType.SWITCH,
            default = "false",
            description = "Enable extended thinking.",
        ),
        ParameterDefinition(
            key = "thinkingBudget",
            label = "Thinking Budget",
            type = ParameterType.TEXT,
            min = 1024.0,
            max = 200000.0,
            default = "",
            description = "Max tokens for thinking (1024-200000).",
        ),
        ParameterDefinition(
            key = "effort",
            label = "Effort",
            type = ParameterType.DROPDOWN,
            options = listOf("", "low", "medium", "high", "max"),
            default = "",
            description = "Controls the overall effort level of the response.",
        ),
        ParameterDefinition(
            key = "fileTokenLimit",
            label = "File Token Limit",
            type = ParameterType.TEXT,
            default = "",
            description = "Maximum number of tokens from attached files.",
        ),
    )

    private fun defaultParams() = listOf(
        ParameterDefinition(
            key = "modelLabel",
            label = "Custom Name",
            type = ParameterType.TEXT,
            default = "",
            description = "Set a custom name for the AI.",
        ),
        ParameterDefinition(
            key = "promptPrefix",
            label = "Custom Instructions",
            type = ParameterType.TEXTAREA,
            default = "",
            description = "Set custom instructions to include in System Message.",
        ),
        ParameterDefinition(
            key = "maxContextTokens",
            label = "Max Context Tokens",
            type = ParameterType.TEXT,
            default = "",
            description = "Max context tokens for this conversation.",
        ),
        ParameterDefinition(
            key = "maxOutputTokens",
            label = "Max Output Tokens",
            type = ParameterType.TEXT,
            default = "",
            description = "Max tokens in the response.",
        ),
        ParameterDefinition(
            key = "temperature",
            label = "Temperature",
            type = ParameterType.SLIDER,
            min = 0.0,
            max = 2.0,
            step = 0.01,
            default = "1.0",
            description = "Controls randomness. Lower values are more focused and deterministic.",
        ),
        ParameterDefinition(
            key = "topP",
            label = "Top P",
            type = ParameterType.SLIDER,
            min = 0.0,
            max = 1.0,
            step = 0.01,
            default = "1.0",
            description = "Nucleus sampling. Controls diversity via cumulative probability cutoff.",
        ),
        ParameterDefinition(
            key = "resendFiles",
            label = "Resend Files",
            type = ParameterType.SWITCH,
            default = "false",
            description = "Resend previously attached files with every message.",
        ),
        ParameterDefinition(
            key = "fileTokenLimit",
            label = "File Token Limit",
            type = ParameterType.TEXT,
            default = "",
            description = "Maximum number of tokens from attached files.",
        ),
    )
}
