import type * as Provider from "./provider"
import { saeeolProviderOptions } from "@/saeeol/provider-options"
import { sdkKey } from "./transform-message"

export function options(input: { model: Provider.Model; sessionID: string; providerOptions?: Record<string, any> }): Record<string, any> {
  const result: Record<string, any> = {}
  if (input.model.api.npm === "@ai-sdk/google-vertex/anthropic" || (!input.model.api.id.includes("claude") && input.model.api.npm === "@ai-sdk/anthropic")) result["toolStreaming"] = false
  if (input.model.providerID === "openai" || input.model.api.npm === "@ai-sdk/openai" || input.model.api.npm === "@ai-sdk/github-copilot") result["store"] = false
  if (input.model.api.npm === "@ai-sdk/azure") { result["store"] = false; result["promptCacheKey"] = input.sessionID }
  if (input.model.api.npm === "@openrouter/ai-sdk-provider" || input.model.api.npm === "@llmgateway/ai-sdk-provider" || input.model.api.npm === "@saeeol/gateway") {
    result["usage"] = { include: true }
    if (input.model.api.id.includes("gemini-3")) result["reasoning"] = { effort: "high" }
  }
  if (input.model.providerID === "baseten" || (input.model.providerID === "saeeol" && ["kimi-k2-thinking", "glm-4.6"].includes(input.model.api.id))) result["chat_template_args"] = { enable_thinking: true }
  if (["zai", "zhipuai"].some((id) => input.model.providerID.includes(id)) && input.model.api.npm === "@ai-sdk/openai-compatible") result["thinking"] = { type: "enabled", clear_thinking: false }
  if (input.model.providerID === "openai" || input.providerOptions?.setCacheKey) result["promptCacheKey"] = input.sessionID
  if (input.model.api.npm === "@ai-sdk/google" || input.model.api.npm === "@ai-sdk/google-vertex") {
    if (input.model.capabilities.reasoning) { result["thinkingConfig"] = { includeThoughts: true }; if (input.model.api.id.includes("gemini-3")) result["thinkingConfig"]["thinkingLevel"] = "high" }
  }
  const modelId = input.model.api.id.toLowerCase()
  if ((input.model.api.npm === "@ai-sdk/anthropic" || input.model.api.npm === "@ai-sdk/google-vertex/anthropic") && (modelId.includes("k2p") || modelId.includes("kimi-k2.") || modelId.includes("kimi-k2p")))
    result["thinking"] = { type: "enabled", budgetTokens: Math.min(16_000, Math.floor(input.model.limit.output / 2 - 1)) }
  if (input.model.providerID === "alibaba-cn" && input.model.capabilities.reasoning && input.model.api.npm === "@ai-sdk/openai-compatible" && !modelId.includes("kimi-k2-thinking")) result["enable_thinking"] = true
  if (input.model.api.id.includes("gpt-5") && !input.model.api.id.includes("gpt-5-chat")) {
    if (!input.model.api.id.includes("gpt-5-pro")) {
      result["reasoningEffort"] = "medium"
      if (["openai", "azure", "copilot"].some((k) => input.model.api.npm.includes(k)) || input.model.api.npm === "@openrouter/ai-sdk-provider" || input.model.api.npm === "@saeeol/gateway") result["reasoningSummary"] = "auto"
    }
    if (["@ai-sdk/openai", "@ai-sdk/azure", "@ai-sdk/github-copilot", "@openrouter/ai-sdk-provider", "@saeeol/gateway"].includes(input.model.api.npm) && input.model.api.id.includes("gpt-5.") && !input.model.api.id.includes("codex") && !input.model.api.id.includes("-chat") && input.model.providerID !== "azure") result["textVerbosity"] = "low"
    if (input.model.providerID.startsWith("saeeol")) { result["promptCacheKey"] = input.sessionID; result["include"] = ["reasoning.encrypted_content"]; result["reasoningSummary"] = "auto" }
  }
  if (input.model.providerID === "venice") result["promptCacheKey"] = input.sessionID
  if (input.model.providerID === "openrouter") result["prompt_cache_key"] = input.sessionID
  if (input.model.api.npm === "@ai-sdk/gateway") result["gateway"] = { caching: "auto" }
  return result
}

export function smallOptions(model: Provider.Model) {
  if (model.providerID === "openai" || model.api.npm === "@ai-sdk/openai" || model.api.npm === "@ai-sdk/github-copilot") {
    if (model.api.id.includes("gpt-5")) return model.api.id.includes("5.") || model.api.id.includes("5-mini") ? { store: false, reasoningEffort: "low" } : { store: false, reasoningEffort: "minimal" }
    return { store: false }
  }
  if (model.providerID === "google") return model.api.id.includes("gemini-3") ? { thinkingConfig: { thinkingLevel: "minimal" } } : { thinkingConfig: { thinkingBudget: 0 } }
  if (model.providerID === "openrouter" || model.providerID === "llmgateway" || model.api.npm === "@saeeol/gateway") return model.api.id.includes("google") ? { reasoning: { enabled: false } } : { reasoningEffort: "minimal" }
  if (model.providerID === "venice") return { veniceParameters: { disableThinking: true } }
  return {}
}

const SLUG_OVERRIDES: Record<string, string> = { amazon: "bedrock" }

export function providerOptions(model: Provider.Model, options: { [x: string]: any }) {
  if (model.api.npm === "@ai-sdk/gateway") {
    const i = model.api.id.indexOf("/"); const rawSlug = i > 0 ? model.api.id.slice(0, i) : undefined; const slug = rawSlug ? (SLUG_OVERRIDES[rawSlug] ?? rawSlug) : undefined
    const gateway = options.gateway; const rest = Object.fromEntries(Object.entries(options).filter(([k]) => k !== "gateway")); const has = Object.keys(rest).length > 0
    const result: Record<string, any> = {}
    if (gateway !== undefined) result.gateway = gateway
    if (has) { if (slug) result[slug] = rest; else if (gateway && typeof gateway === "object" && !Array.isArray(gateway)) result.gateway = { ...gateway, ...rest }; else result.gateway = rest }
    return result
  }
  if (model.api.npm === "@saeeol/gateway") return saeeolProviderOptions(options)
  const usesDotSplitOptions = model.api.npm === "@ai-sdk/openai-compatible" || model.api.npm === "@ai-sdk/openai" || model.api.npm === "@ai-sdk/anthropic"
  const key = sdkKey(model.api.npm) ?? (usesDotSplitOptions ? model.providerID.split(".")[0] : model.providerID)
  if (model.api.npm === "@ai-sdk/azure") return { openai: options, azure: options }
  return { [key]: options }
}
