import { sdk } from '../sdk'
import { configFile } from '../file-models/config.json'

const { InputSpec, Value } = sdk

const inputSpec = InputSpec.of({
  openai_compatible_base_url: Value.text({
    name: 'Base URL',
    description:
      'OpenAI-compatible API endpoint. Examples: https://api.deepseek.com/v1, https://api.together.xyz/v1, https://api.groq.com/openai/v1. Must include the /v1 (or equivalent) path segment.',
    required: true,
    default: null,
    minLength: 1,
    maxLength: 512,
    patterns: [
      {
        regex: '^https?://.+',
        description: 'Must start with http:// or https://',
      },
    ],
  }),
  openai_compatible_api_key: Value.text({
    name: 'API Key',
    description:
      'API key for the OpenAI-compatible backend. Some self-hosted backends accept any non-empty value — leave blank for those.',
    required: false,
    default: null,
    masked: true,
    minLength: 0,
    maxLength: 256,
  }),
})

export const setOpenAICompatible = sdk.Action.withInput(
  'set-openai-compatible',

  async ({ effects }) => ({
    name: 'Set OpenAI-Compatible Backend',
    description:
      'Point Recaps at any OpenAI-compatible chat-completions API: DeepSeek, Together, Groq, Fireworks, self-hosted vLLM, etc. Used for topic analysis only — does not transcribe audio.',
    warning: null,
    allowedStatuses: 'any',
    group: 'AI Providers',
    visibility: 'enabled',
  }),

  inputSpec,

  async ({ effects }) => {
    const config = await configFile.read().once()
    return {
      openai_compatible_base_url: config?.openai_compatible_base_url || undefined,
      openai_compatible_api_key: config?.openai_compatible_api_key || undefined,
    }
  },

  async ({ effects, input }) => {
    await configFile.merge(effects, {
      openai_compatible_base_url: (input.openai_compatible_base_url || '').trim(),
      openai_compatible_api_key: (input.openai_compatible_api_key || '').trim(),
    })
    return null
  },
)
