/*
 * Copyright 2017-2025 noear.org and authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.noear.solon.ai.chat;

import org.noear.solon.Utils;
import org.noear.solon.ai.AiModel;
import org.noear.solon.ai.chat.dialect.ChatDialect;
import org.noear.solon.ai.chat.dialect.ChatDialectManager;
import org.noear.solon.ai.chat.interceptor.ChatInterceptor;
import org.noear.solon.ai.chat.prompt.Prompt;
import org.noear.solon.ai.chat.prompt.Prompt;
import org.noear.solon.ai.chat.skill.Skill;
import org.noear.solon.ai.chat.skill.SkillProvider;
import org.noear.solon.ai.chat.tool.*;
import org.noear.solon.ai.chat.message.ChatMessage;
import org.noear.solon.core.Props;
import org.noear.solon.core.util.Assert;
import org.noear.solon.lang.Preview;

import java.lang.reflect.Type;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.time.Duration;
import java.util.*;
import java.util.function.Consumer;

/**
 * 聊天模型
 *
 * @author noear
 * @since 3.1
 */
@Preview("3.1")
public class ChatModel implements AiModel {
    private final ChatConfig config;
    private final ChatConfigReadonly configReadonly;
    private final ChatDialect dialect;

    public ChatModel(Properties properties) {
        //支持直接注入
        this(Props.from(properties).bindTo(new ChatConfig()));
    }

    public ChatModel(ChatConfig config) {
        Assert.notNull(config, "The config is required");
        Assert.notNull(config.getApiUrl(), "The config.apiUrl is required");
        Assert.notNull(config.getModel(), "The config.model is required");

        this.config = config;
        this.configReadonly = new ChatConfigReadonly(config);
        this.dialect = ChatDialectManager.select(config);

        Assert.notNull(dialect, "The dialect(provider) no matched, check config or dependencies");

    }

    public ChatDialect getDialect() {
        return dialect;
    }

    public ChatConfigReadonly getConfig() {
        return configReadonly;
    }

    public String getNameOrModel(){
        return config.getNameOrModel();
    }

    public String getModel(){
        return config.getModel();
    }

    public String getProvider(){
        return config.getProvider();
    }

    /**
     * 提示语
     *
     * @deprecated 3.8.4 {@link ChatRequestDesc#session(ChatSession)}
     */
    @Deprecated
    public ChatRequestDesc prompt(ChatSession session) {
        return new ChatRequestDescDefault(config, dialect, session, null);
    }

    /**
     * 提示语
     */
    public ChatRequestDesc prompt(Prompt prompt) {
        return new ChatRequestDescDefault(config, dialect, null, prompt);
    }

    /**
     * 提示语
     */
    public ChatRequestDesc prompt(List<ChatMessage> messages) {
        return prompt(Prompt.of(messages));
    }

    /**
     * 提示语
     */
    public ChatRequestDesc prompt(ChatMessage... messages) {
        return prompt(Utils.asList(messages));
    }

    /**
     * 提示语
     */
    public ChatRequestDesc prompt(String content) {
        return prompt(ChatMessage.ofUser(content));
    }


    @Override
    public String toString() {
        return "ChatModel{" +
                "config=" + config +
                ", dialect=" + dialect.getClass().getName() +
                '}';
    }


    /// /////////////////////////////////

    /**
     * 构建
     */
    public static Builder of(ChatConfig config) {
        return new Builder(config);
    }

    /**
     * 开始构建
     */
    public static Builder of(String apiUrl) {
        return new Builder(apiUrl);
    }

    /// //////////////////

    /**
     * 聊天模型构建器实现
     *
     * @author noear
     * @since 3.1
     */
    public static class Builder {
        private final ChatConfig config;

        /**
         * @param apiUrl 接口地址
         */
        public Builder(String apiUrl) {
            this.config = new ChatConfig();
            this.config.setApiUrl(apiUrl);
        }

        /**
         * @param config 配置
         */
        public Builder(ChatConfig config) {
            this.config = config;
        }

        /**
         * 接口密钥
         */
        public Builder apiKey(String apiKey) {
            config.setApiKey(apiKey);
            return this;
        }

        /**
         * 服务提供者
         */
        public Builder provider(String provider) {
            config.setProvider(provider);
            return this;
        }

        /**
         * 使用模型
         */
        public Builder model(String model) {
            config.setModel(model);
            return this;
        }

        /**
         * 使用模型
         */
        public Builder contextLength(long contextLength) {
            config.setContextLength(contextLength);
            return this;
        }

        /**
         * 头信息添加
         */
        public Builder headerSet(String key, String value) {
            config.setHeader(key, value);
            return this;
        }

        /**
         * User-Agent
         */
        public Builder userAgent(String userAgent) {
            config.setUserAgent(userAgent);
            return this;
        }

        /**
         * 角色
         */
        public Builder role(String role) {
            config.getModelOptions().role(role);
            return this;
        }

        /**
         * 指令
         */
        public Builder instruction(String instruction) {
            config.getModelOptions().instruction(instruction);
            return this;
        }

        /**
         * 系统提示词
         */
        public Builder systemPrompt(String systemPrompt) {
            config.getModelOptions().systemPrompt(systemPrompt);
            return this;
        }

        public Builder outputSchema(String outputSchema) {
            config.getModelOptions().outputSchema(outputSchema);
            return this;
        }

        public Builder outputSchema(Type type) {
            config.getModelOptions().outputSchema(type);
            return this;
        }

        /**
         * 模型选项
         */
        public Builder modelOptions(Consumer<ModelOptionsAmend<?, ChatInterceptor>> consumer) {
            consumer.accept(config.getModelOptions());
            return this;
        }

        public Builder reasoningFieldName(String reasoningFieldName) {
            config.setReasoningFieldName(reasoningFieldName);
            return this;
        }

        /**
         * 默认工具添加（即每次请求都会带上）
         *
         * @param tools 工具
         */
        public Builder defaultToolAdd(FunctionTool... tools) {
            config.addDefaultTool(tools);
            return this;
        }

        /**
         * 默认工具添加（即每次请求都会带上）
         *
         * @param toolColl 工具集合
         */
        public Builder defaultToolAdd(Iterable<FunctionTool> toolColl) {
            for (FunctionTool f : toolColl) {
                config.addDefaultTool(f);
            }

            return this;
        }

        /**
         * 默认工具添加（即每次请求都会带上）
         *
         * @param toolProvider 工具提供者
         */
        public Builder defaultToolAdd(ToolProvider toolProvider) {
            return defaultToolAdd(toolProvider.getTools());
        }

        /**
         * 默认工具添加（即每次请求都会带上）
         *
         * @param toolObj 工具对象
         * @deprecated 3.10.1
         */
        @Deprecated
        public Builder defaultToolAdd(Object toolObj) {
            config.getModelOptions().toolAdd(toolObj);
            return this;
        }

        /**
         * 默认工具添加（即每次请求都会带上）
         *
         * @param name        名字
         * @param toolBuilder 工具构建器
         */
        public Builder defaultToolAdd(String name, Consumer<FunctionToolDesc> toolBuilder) {
            FunctionToolDesc decl = new FunctionToolDesc(name);
            toolBuilder.accept(decl);
            config.getModelOptions().toolAdd(decl);
            return this;
        }


        /**
         * 默认工具包添加
         *
         * @since 3.8.4
         */
        public Builder defaultSkillAdd(Skill... skill) {
            for (Skill s : skill) {
                defaultSkillAdd(0, s);
            }
            return this;
        }

        /**
         * 默认工具包添加
         *
         * @since 3.9.5
         */
        public Builder defaultSkillAdd(SkillProvider skillProvider) {
            for (Skill s : skillProvider.getSkills()) {
                defaultSkillAdd(0, s);
            }
            return this;
        }

        /**
         * 默认工具包添加
         *
         * @since 3.8.4
         */
        public Builder defaultSkillAdd(int index, Skill skill) {
            config.addDefaultSkill(index, skill);
            return this;
        }

        /**
         * 添加默认拦截器
         *
         * @param interceptor 拦截器
         */
        public Builder defaultInterceptorAdd(ChatInterceptor interceptor) {
            return defaultInterceptorAdd(0, interceptor);
        }

        /**
         * 添加默认拦截器
         *
         * @param index       顺序位
         * @param interceptor 拦截器
         */
        public Builder defaultInterceptorAdd(int index, ChatInterceptor interceptor) {
            config.addDefaultInterceptor(index, interceptor);
            return this;
        }

        /**
         * 网络超时
         */
        public Builder timeout(Duration timeout) {
            config.setTimeout(timeout);

            return this;
        }

        /**
         * 网络代理
         */
        public Builder proxy(Proxy proxy) {
            config.setProxyInstance(proxy);

            return this;
        }

        /**
         * 网络代理
         */
        public Builder proxy(String host, int port) {
            return proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(host, port)));
        }

        /**
         * 构建
         */
        public ChatModel build() {
            return new ChatModel(config);
        }

        //----------------

        /**
         * 添加默认选项
         *
         * @deprecated 3.8.4 {@link #modelOptions(Consumer)}
         */
        @Deprecated
        public Builder defaultOptionAdd(String key, Object val) {
            config.addDefaultOption(key, val);
            return this;
        }


        /**
         * 默认工具添加（即每次请求都会带上）
         *
         * @param tool 工具对象
         * @deprecated 3.8.4 {@link #defaultToolAdd(FunctionTool)}
         */
        @Deprecated
        public Builder defaultToolsAdd(FunctionTool tool) {
            return defaultToolAdd(tool);
        }

        /**
         * 默认工具添加（即每次请求都会带上）
         *
         * @param toolColl 工具集合
         * @deprecated 3.8.4 {@link #defaultToolAdd(Iterable)}
         */
        @Deprecated
        public Builder defaultToolsAdd(Iterable<FunctionTool> toolColl) {
            return defaultToolAdd(toolColl);
        }

        /**
         * 默认工具添加（即每次请求都会带上）
         *
         * @param toolProvider 工具提供者
         * @deprecated 3.8.4 {@link #defaultToolAdd(ToolProvider)}
         */
        @Deprecated
        public Builder defaultToolsAdd(ToolProvider toolProvider) {
            return defaultToolAdd(toolProvider.getTools());
        }

        /**
         * 默认工具添加（即每次请求都会带上）
         *
         * @param toolObj 工具对象
         * @deprecated 3.8.4 {@link #defaultToolAdd(Object)}
         */
        @Deprecated
        public Builder defaultToolsAdd(Object toolObj) {
            return defaultToolAdd(toolObj);
        }

        /**
         * 默认工具添加（即每次请求都会带上）
         *
         * @param name        名字
         * @param toolBuilder 工具构建器
         * @deprecated 3.8.4 {@link #defaultToolAdd(String, Consumer)}
         */
        @Deprecated
        public Builder defaultToolsAdd(String name, Consumer<FunctionToolDesc> toolBuilder) {
            return defaultToolAdd(name, toolBuilder);
        }


        /**
         * 默认工具上下文添加
         *
         * @deprecated 3.8.4 {@link #modelOptions(Consumer)}
         */
        @Deprecated
        public Builder defaultToolsContextAdd(String key, Object value) {
            config.getModelOptions().toolContextPut(key, value);
            return this;
        }

        /**
         * 默认工具上下文添加
         *
         * @deprecated 3.8.4 {@link #modelOptions(Consumer)}
         */
        @Deprecated
        public Builder defaultToolsContextAdd(Map<String, Object> map) {
            config.getModelOptions().toolContextPut(map);
            return this;
        }
    }
}