package com.sino.mci.inbound.intent;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 手动装配 OpenAI ChatClient。
 *
 * <p>Spring AI 1.0.0-M6 的 {@code OpenAiAutoConfiguration} 引用了 Spring Boot 4.1 中
 * 不再存在的 {@code RestClientAutoConfiguration}，因此排除官方自动配置；本配置在
 * {@code spring.ai.openai.api-key} 存在时创建一个基于 {@code RestClient} 的
 * {@link ChatModel}，并据此生成 {@link ChatClient.Builder}。</p>
 */
@Configuration
@ConditionalOnExpression("'${spring.ai.openai.api-key:}'.trim() != ''")
public class OpenAiChatClientConfig {

    @Value("${spring.ai.openai.api-key}")
    private String apiKey;

    @Value("${spring.ai.openai.base-url:https://api.openai.com}")
    private String baseUrl;

    @Value("${spring.ai.openai.chat.options.model:gpt-4o-mini}")
    private String model;

    @Bean
    public ChatModel openAiChatModel() {
        return new OpenAiRestChatModel(baseUrl, apiKey, model);
    }

    @Bean
    public ChatClient.Builder openAiChatClientBuilder(ChatModel openAiChatModel) {
        return ChatClient.builder(openAiChatModel);
    }
}
