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

const { InputSpec, Value } = sdk

const inputSpec = InputSpec.of({
  anthropic_api_key: Value.text({
    name: 'Anthropic API Key',
    description:
      'Your Anthropic (Claude) API key. Get one at console.anthropic.com. Required to use Claude models for topic analysis.',
    required: true,
    default: null,
    masked: true,
    minLength: 1,
    maxLength: 256,
  }),
})

export const setAnthropicApiKey = sdk.Action.withInput(
  'set-anthropic-api-key',

  async ({ effects }) => ({
    name: 'Set Anthropic API Key',
    description:
      'Configure your Anthropic (Claude) API key for topic analysis. Claude does not transcribe audio — pair it with Gemini or OpenAI Whisper for transcription.',
    warning: null,
    allowedStatuses: 'any',
    group: 'AI Providers',
    visibility: 'enabled',
  }),

  inputSpec,

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

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