package com.ruoyi.web.kb.support;

import org.springframework.boot.context.properties.ConfigurationProperties;

/**
 * 大模型网关 HTTP 对接（知识库 {@code /api/v1/kb/**}、对话 {@code /v1/models}、{@code /v1/chat/completions}），对应 {@code ruoyi.kb}。
 */
@ConfigurationProperties(prefix = "ruoyi.kb")
public class KbApiProperties
{
    /** 是否启用远程调用（为 false 时同步/移出仅改本地，便于无对端环境） */
    private boolean enabled = true;

    /** 协议，如 http、https */
    private String scheme = "http";

    private String host = "localhost";

    private int port = 9107;

    /** 访问令牌（配置原始 key，请求头自动加 Bearer 前缀） */
    private String apiKey = "";

    /** 鉴权请求头名 */
    private String authHeaderName = "Authorization";

    /**
     * 当业务表 {@code content_file_url} 不是以 http(s) 开头时，与其拼接成知识库可拉取的完整 URL。
     * 例如 {@code http://业务网关域名:端口}。
     */
    private String fileUrlBase = "";

    /** 方式一上传默认知识库标识 */
    private String defaultKnowledgeBase = "default";

    private int connectTimeoutMs = 15000;

    private int readTimeoutMs = 120000;

    /** 对话流式 SSE（/v1/chat/completions stream=true）读超时 */
    private int streamReadTimeoutMs = 300000;

    /** 知识库同步/移出失败或超时时的最大重试次数（不含首次请求） */
    private int maxRetries = 2;

    /** 重试间隔（毫秒） */
    private int retryIntervalMs = 500;

    public boolean isEnabled()
    {
        return enabled;
    }

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

    public String getScheme()
    {
        return scheme;
    }

    public void setScheme(String scheme)
    {
        this.scheme = scheme;
    }

    public String getHost()
    {
        return host;
    }

    public void setHost(String host)
    {
        this.host = host;
    }

    public int getPort()
    {
        return port;
    }

    public void setPort(int port)
    {
        this.port = port;
    }

    public String getApiKey()
    {
        return apiKey;
    }

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

    public String getAuthHeaderName()
    {
        return authHeaderName;
    }

    public void setAuthHeaderName(String authHeaderName)
    {
        this.authHeaderName = authHeaderName;
    }

    /**
     * 请求头鉴权值，默认 {@code Bearer {apiKey}}。
     */
    public String authHeaderValue()
    {
        if (apiKey == null || apiKey.isEmpty())
        {
            return "";
        }
        String k = apiKey.trim();
        if (k.regionMatches(true, 0, "Bearer ", 0, 7))
        {
            return k;
        }
        return "Bearer " + k;
    }

    public String getFileUrlBase()
    {
        return fileUrlBase;
    }

    public void setFileUrlBase(String fileUrlBase)
    {
        this.fileUrlBase = fileUrlBase;
    }

    public String getDefaultKnowledgeBase()
    {
        return defaultKnowledgeBase;
    }

    public void setDefaultKnowledgeBase(String defaultKnowledgeBase)
    {
        this.defaultKnowledgeBase = defaultKnowledgeBase;
    }

    public int getConnectTimeoutMs()
    {
        return connectTimeoutMs;
    }

    public void setConnectTimeoutMs(int connectTimeoutMs)
    {
        this.connectTimeoutMs = connectTimeoutMs;
    }

    public int getReadTimeoutMs()
    {
        return readTimeoutMs;
    }

    public void setReadTimeoutMs(int readTimeoutMs)
    {
        this.readTimeoutMs = readTimeoutMs;
    }

    public int getStreamReadTimeoutMs()
    {
        return streamReadTimeoutMs;
    }

    public void setStreamReadTimeoutMs(int streamReadTimeoutMs)
    {
        this.streamReadTimeoutMs = streamReadTimeoutMs;
    }

    public int getMaxRetries()
    {
        return maxRetries;
    }

    public void setMaxRetries(int maxRetries)
    {
        this.maxRetries = maxRetries;
    }

    public int getRetryIntervalMs()
    {
        return retryIntervalMs;
    }

    public void setRetryIntervalMs(int retryIntervalMs)
    {
        this.retryIntervalMs = retryIntervalMs;
    }

    public String baseUrl()
    {
        return scheme + "://" + host + ":" + port;
    }
}
