import type { ActionId } from '@lib/actions';
import { callProvider, defaultMaxTokens, getSystemPrompt } from '@lib/providers';
import type { LexAIConfig, PromptParams } from '@lib/types';
import type { WritingStyle } from '@lib/actions';

export interface GenerateOptions {
  action: ActionId;
  text: string;
  config: LexAIConfig;
  writingStyle?: WritingStyle;
  promptParams?: PromptParams;
  promptModel?: string;
}

export async function generateSuggestion(opts: GenerateOptions): Promise<{
  result?: string;
  error?: string;
}> {
  const style = opts.action === 'prompt' ? undefined : opts.writingStyle;
  const promptParams = opts.action === 'prompt' ? opts.promptParams : undefined;
  const systemPrompt = getSystemPrompt(opts.action, style, promptParams);
  const config =
    opts.action === 'prompt' && opts.promptModel
      ? { ...opts.config, model: opts.promptModel }
      : opts.config;
  const callOpts =
    opts.action === 'prompt'
      ? { maxTokens: Math.max(2048, defaultMaxTokens(opts.text)) }
      : undefined;
  return callProvider(config, opts.text, systemPrompt, callOpts);
}
