package com.huimv.employment.integration.kb;

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

/**
 * 大模型网关 HTTP 对接（智能体 {@code /api/console/chat}、RAG {@code /api/rag/**}），绑定 {@code fe.kb.*}。
 */
@ConfigurationProperties(prefix = "fe.kb")
public class KbApiProperties {

    /** 是否启用远程调用（为 false 时接口返回业务错误，便于无对端环境） */
    private boolean enabled = true;

    private String scheme = "http";

    private String host = "localhost";

    private int port = 9188;

    /** 访问令牌；配置后上游请求带 Authorization: Bearer {apiKey} */
    private String apiKey = "";

    private String authHeaderName = "Authorization";

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

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

    /** 默认模型标识（预留） */
    private String defaultModel = "default";

    /** 上游 {@code X-Agent-Id} 默认值（见智能体接口文档） */
    private String defaultAgentId = "default";

    /**
     * Markdown 模板与生成文件根目录（对应若依 ruoyi.profile）。
     * 为空时 {@link KbMarkdownGenerator} 不可用。
     */
    private String uploadPath = "";

    /**
     * 可选：正文 URL 含此外网域名时，改写为 {@link #urlRewriteToHost} 供知识库节点内网拉取。
     */
    private String urlRewriteFromHost = "";

    /** 可选：URL 改写目标 host（含端口，如 192.168.1.1:443） */
    private String urlRewriteToHost = "";

    private int connectTimeoutMs = 15000;

    private int readTimeoutMs = 120000;

    /** 对话流式 SSE（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;
    }

    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 String getDefaultModel() {
        return defaultModel;
    }

    public void setDefaultModel(String defaultModel) {
        this.defaultModel = defaultModel;
    }

    public String getDefaultAgentId() {
        return defaultAgentId;
    }

    public void setDefaultAgentId(String defaultAgentId) {
        this.defaultAgentId = defaultAgentId;
    }

    public String getUploadPath() {
        return uploadPath;
    }

    public void setUploadPath(String uploadPath) {
        this.uploadPath = uploadPath;
    }

    public String getUrlRewriteFromHost() {
        return urlRewriteFromHost;
    }

    public void setUrlRewriteFromHost(String urlRewriteFromHost) {
        this.urlRewriteFromHost = urlRewriteFromHost;
    }

    public String getUrlRewriteToHost() {
        return urlRewriteToHost;
    }

    public void setUrlRewriteToHost(String urlRewriteToHost) {
        this.urlRewriteToHost = urlRewriteToHost;
    }

    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;
    }

    /** 是否配置了上游 Bearer 鉴权。 */
    public boolean hasUpstreamAuth() {
        return apiKey != null && !apiKey.trim().isEmpty();
    }

    /** 配置了 api-key 时写入 Authorization 请求头。 */
    public void applyAuthHeader(HttpHeaders headers) {
        if (hasUpstreamAuth()) {
            headers.add(authHeaderName, authHeaderValue());
        }
    }

    /** 请求头鉴权值，默认 {@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 baseUrl() {
        return scheme + "://" + host + ":" + port;
    }
}
