import { PromptType } from "../types";

import convertToolChoiceForBackend from "./convertToolChoiceForBackend";
import filterNullParams from "./filterNullParams";

import { CreateAgenticPromptRequest, ModelProvider } from "@/lib/api-client/api-client";

const toBackendPrompt = (prompt: PromptType): CreateAgenticPromptRequest => ({
  model_name: prompt.modelName,
  model_provider: prompt.modelProvider as ModelProvider,
  messages: prompt.messages.map((msg) => ({
    role: msg.role,
    content: msg.content,
    tool_call_id: msg.tool_call_id ?? null,
    tool_calls: msg.tool_calls
      ? msg.tool_calls.map((tc) => ({
          id: tc.id,
          type: tc.type ?? "function",
          function: {
            name: tc.function.name,
            arguments: tc.function.arguments || "",
          },
        }))
      : null,
  })),
  tools: prompt.tools.map((tool) => ({
    function: {
      name: tool.function.name,
      description: tool.function.description,
      parameters: tool.function.parameters,
    },
    strict: tool.strict,
    type: tool.type,
  })),
  config: filterNullParams({
    tool_choice: convertToolChoiceForBackend(prompt.toolChoice, prompt.tools),
    temperature: prompt.modelParameters.temperature,
    top_p: prompt.modelParameters.top_p,
    timeout: prompt.modelParameters.timeout,
    stream_options: prompt.modelParameters.stream_options,
    max_tokens: prompt.modelParameters.max_tokens,
    max_completion_tokens: prompt.modelParameters.max_completion_tokens,
    frequency_penalty: prompt.modelParameters.frequency_penalty,
    presence_penalty: prompt.modelParameters.presence_penalty,
    stop: prompt.modelParameters.stop,
    seed: prompt.modelParameters.seed,
    reasoning_effort: prompt.modelParameters.reasoning_effort,
    logprobs: prompt.modelParameters.logprobs,
    top_logprobs: prompt.modelParameters.top_logprobs,
    logit_bias: prompt.modelParameters.logit_bias,
    thinking: prompt.modelParameters.thinking,
    response_format: prompt.responseFormat ? JSON.parse(prompt.responseFormat) : null,
  }),
});

export const toBackendPromptBaseConfig = (prompt: PromptType): CreateAgenticPromptRequest => {
  return toBackendPrompt(prompt);
};

export default toBackendPrompt;
