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

const { InputSpec, Value } = sdk

const inputSpec = InputSpec.of({
  gemini_api_key: Value.text({
    name: 'Gemini API Key',
    description:
      'Your Google Gemini API key. Get a free key at aistudio.google.com/apikey',
    required: true,
    default: null,
    masked: true,
    minLength: 1,
    maxLength: 256,
  }),
})

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

  async ({ effects }) => ({
    name: 'Set Gemini API Key',
    description:
      'Configure your Google Gemini API key for transcription and analysis',
    warning: null,
    allowedStatuses: 'any',
    group: 'AI Providers',
    visibility: 'enabled',
  }),

  inputSpec,

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

  async ({ effects, input }) => {
    await configFile.merge(effects, { gemini_api_key: input.gemini_api_key })
    return null
  },
)
