import axios from 'axios';
import { ChatMessage, ChatOptions, ProviderResponse } from '../../../types/chat';
import { Tool } from '../../../types/tool';
import { AzureConfig } from './config';
import { AzureChatRequestBody, AzureChatResponse } from './types';
import { convertToolSchema, convertMessages, convertToolCall } from './tools';

export async function chat(
  config: AzureConfig,
  messages: ChatMessage[],
  options: ChatOptions,
  tools?: Tool[]
): Promise<ProviderResponse> {
  const url = config.getApiUrl('chat/completions');
  
  const requestBody: AzureChatRequestBody = {
    messages: convertMessages(messages),
    max_tokens: options.maxTokens,
    temperature: options.temperature,
  };

  if (tools && tools.length > 0) {
    requestBody.tools = tools
        .map(tool => convertToolSchema(tool.getSchema()))
        .map(tool => ({
            type: 'function',
            function: tool
        }));
    //requestBody.tool_choice = 'auto';
  }

  const response = await axios.post<AzureChatResponse>(url, requestBody, {
    headers: {
      'Content-Type': 'application/json',
      'api-key': config.apiKey,
    },
  });

  return createProviderResponse(response.data);
}

function createProviderResponse(data: AzureChatResponse): ProviderResponse {
  const choice = data.choices && data.choices.length > 0 ? data.choices[0] : null;
  if (!choice) {
    throw new Error('Unexpected response format from Azure OpenAI API');
  }

  const response: ProviderResponse = {
    content: choice.message?.content || '',
    toolCalls: [],
    usage: data.usage ? {
      promptTokens: data.usage.prompt_tokens,
      completionTokens: data.usage.completion_tokens,
      totalTokens: data.usage.total_tokens
    } : undefined,
  };

  if (choice.message?.function_call) {
    response.toolCalls = [convertToolCall(choice.message.function_call)];
  }

  return response;
}