/*
 * 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;

import org.noear.solon.Utils;
import org.noear.solon.ai.util.ProxyDesc;
import org.noear.solon.core.util.Assert;
import org.noear.solon.lang.Nullable;
import org.noear.solon.net.http.HttpUtils;
import org.noear.solon.net.http.impl.HttpSslSupplierAny;

import java.net.InetSocketAddress;
import java.net.Proxy;
import java.time.Duration;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Ai 接口配置
 *
 * @author noear
 * @since 3.1
 */
public class AiConfig {
    //用于管理显示
    protected @Nullable String name;
    protected @Nullable String description;
    protected long contextLength;
    protected boolean enabled = true;

    protected String apiUrl;
    protected String apiKey;
    protected String provider;
    protected String model;
    protected final Map<String, String> headers = new LinkedHashMap<>();
    protected String userAgent;
    protected Duration timeout = Duration.ofSeconds(60);
    protected ProxyDesc proxy; //给配置用
    protected transient Proxy proxyInstance; //给代码用



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


    public String getName() {
        return name;
    }


    public String getNameOrModel() {
        if(Assert.isEmpty(name)) {
            return model;
        }

        return name;
    }

    public String getDescription() {
        return description;
    }

    public String getDescriptionOrModel() {
        if(Assert.isEmpty(description)) {
            return model;
        }

        return description;
    }

    public String getApiKey() {
        return apiKey;
    }

    public String getApiUrl() {
        return apiUrl;
    }

    public String getProvider() {
        return provider;
    }

    public String getModel() {
        return model;
    }

    public long getContextLength() {
        return contextLength;
    }

    public Map<String, String> getHeaders() {
        return headers;
    }

    public String getUserAgent() {
        return userAgent;
    }

    public Duration getTimeout() {
        return timeout;
    }

    public Proxy getProxy() {
        if (proxyInstance == null) {
            if (proxy != null) {
                proxyInstance = new Proxy(
                        Proxy.Type.valueOf(proxy.type),
                        new InetSocketAddress(proxy.host, proxy.port));
            }
        }

        return proxyInstance;
    }

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

    public void setName(String name) {
        this.name = name;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public void setApiUrl(String apiUrl) {
        this.apiUrl = apiUrl;
    }

    public void setApiKey(String apiKey) {
        this.apiKey = apiKey;
    }

    public void setProvider(String provider) {
        this.provider = provider;
    }

    public void setModel(String model) {
        this.model = model;
    }

    public void setContextLength(long contextLength) {
        this.contextLength = contextLength;
    }

    public void setHeaders(Map<String, String> headers) {
        if (headers != null) {
            this.headers.putAll(headers);
        }
    }

    public void setHeader(String key, String value) {
        headers.put(key, value);
    }

    public void setUserAgent(String userAgent) {
        this.userAgent = userAgent;
    }

    public void setTimeout(Duration timeout) {
        if (timeout != null) {
            this.timeout = timeout;
        }
    }

    public void setProxyInstance(Proxy proxyInstance) {
        this.proxyInstance = proxyInstance;
        this.proxy = null;
    }

    public boolean isEnabled() {
        return enabled;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    /**
     * 创建 http 请求
     */
    public HttpUtils createHttpUtils() {
        HttpUtils httpUtils = HttpUtils
                .http(getApiUrl())
                .ssl(HttpSslSupplierAny.getInstance())
                .timeout((int) getTimeout().getSeconds());

        if (getProxy() != null) {
            httpUtils.proxy(getProxy());
        }

        if (Utils.isNotEmpty(getApiKey())) {
            httpUtils.header("Authorization", "Bearer " + getApiKey());
        }

        if (Utils.isNotEmpty(getUserAgent())) {
            httpUtils.userAgent(getUserAgent());
        }

        httpUtils.headers(getHeaders());

        return httpUtils;
    }

    @Override
    public String toString() {
        return "AiConfig{" +
                "apiUrl='" + apiUrl + '\'' +
                ", apiKey='" + apiKey + '\'' +
                ", provider='" + provider + '\'' +
                ", model='" + model + '\'' +
                ", headers=" + headers +
                ", timeout=" + timeout +
                ", proxy=" + getProxy() +
                '}';
    }
}