/**
 * Derive per-request capability flags + provider-builtin web search config
 * from (model, provider, assistant).
 *
 * Replaces the capability-detection half of the dead `parameterBuilder.ts`.
 * Read by `agentParams/features/*` to gate plugins like
 * `providerToolPlugin('webSearch' / 'urlContext')` and to let callers
 * set `streamOutput` / tool-use flags without duplicating these checks.
 */

import { application } from '@application'
import type { WebSearchPluginConfig } from '@cherrystudio/ai-core/built-in/plugins'
import { extensionRegistry } from '@cherrystudio/ai-core/provider'
import type { Assistant } from '@shared/data/types/assistant'
import type { Model } from '@shared/data/types/model'
import type { Provider } from '@shared/data/types/provider'
import {
  isAnthropicModel,
  isFixedReasoningModel,
  isFunctionCallingModel,
  isGeminiModel,
  isGenerateImageModel,
  isGrokModel,
  isOpenAIModel,
  isOpenRouterBuiltInWebSearchModel,
  isPureGenerateImageModel,
  isSupportedReasoningEffortModel,
  isSupportedThinkingTokenModel,
  isWebSearchModel
} from '@shared/utils/model'
import { isAIGatewayProvider, isSupportUrlContextProvider } from '@shared/utils/provider'
import { SystemProviderIds } from '@shared/utils/systemProviderId'

import { getAiSdkProviderId } from '../../../provider/factory'
import type { AppProviderId } from '../../../types'
import { buildProviderBuiltinWebSearchConfig } from '../../../utils/websearch'

export interface ResolvedCapabilities {
  enableReasoning: boolean
  enableWebSearch: boolean
  enableUrlContext: boolean
  enableGenerateImage: boolean
  isSupportedToolUse: boolean
  streamOutput: boolean
  webSearchPluginConfig?: WebSearchPluginConfig
}

export interface ResolveCapabilitiesOptions {
  /** Caller-supplied external web search provider id. When set, disables built-in web search. */
  webSearchProviderId?: string
}

function mapVertexAIGatewayModelToProviderId(model: Model): AppProviderId | undefined {
  if (isAnthropicModel(model)) return 'anthropic'
  if (isGeminiModel(model)) return 'google'
  if (isGrokModel(model)) return 'xai'
  if (isOpenAIModel(model)) return 'openai'
  return undefined
}

export function resolveCapabilities(
  model: Model,
  provider: Provider,
  assistant: Assistant,
  options: ResolveCapabilitiesOptions = {}
): ResolvedCapabilities {
  // `isFixedReasoningModel` covers models where reasoning is always on regardless
  // of user setting (e.g. OpenAI o1 / o3 — they reason by construction).
  const enableReasoning =
    ((isSupportedThinkingTokenModel(model) || isSupportedReasoningEffortModel(model)) &&
      assistant.settings?.reasoning_effort !== undefined) ||
    isFixedReasoningModel(model)

  const hasExternalSearch = !!options.webSearchProviderId
  const enableWebSearch =
    !hasExternalSearch &&
    ((!!assistant.settings?.enableWebSearch && isWebSearchModel(model)) ||
      isOpenRouterBuiltInWebSearchModel(model) ||
      model.id.includes('sonar'))

  // `assistant.enableUrlContext` / `enableGenerateImage` are not yet on the
  // shared `Assistant` schema, so the toggles stay guarded and default to false.
  const urlContextSupported =
    isSupportUrlContextProvider(provider) &&
    !isPureGenerateImageModel(model) &&
    (isGeminiModel(model) || isAnthropicModel(model))
  const enableUrlContext = urlContextSupported && false

  const enableGenerateImage = isGenerateImageModel(model) && false

  const isSupportedToolUse = isFunctionCallingModel(model)

  const streamOutput = assistant.settings?.streamOutput !== false

  // Build provider-builtin web search config when enabled
  let webSearchPluginConfig: WebSearchPluginConfig | undefined
  if (enableWebSearch) {
    const preferenceService = application.get('PreferenceService')
    const webSearchConfig = {
      maxResults: preferenceService.get('chat.web_search.max_results'),
      excludeDomains: preferenceService.get('chat.web_search.exclude_domains')
    }
    const aiSdkProviderId = getAiSdkProviderId(provider, model)
    if (extensionRegistry.has(aiSdkProviderId)) {
      webSearchPluginConfig = buildProviderBuiltinWebSearchConfig(aiSdkProviderId, webSearchConfig, model)
    } else if (isAIGatewayProvider(provider) || provider.id === SystemProviderIds.gateway) {
      const gatewayProviderId = mapVertexAIGatewayModelToProviderId(model)
      if (gatewayProviderId) {
        webSearchPluginConfig = buildProviderBuiltinWebSearchConfig(gatewayProviderId, webSearchConfig, model)
      }
    }
  }

  return {
    enableReasoning,
    enableWebSearch,
    enableUrlContext,
    enableGenerateImage,
    isSupportedToolUse,
    streamOutput,
    webSearchPluginConfig
  }
}
