/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2024-2025. All rights reserved.
 */
package magic.config

import magic.core.model.{ChatModel, EmbeddingModel}
import magic.utils.{exists, setEnv, getEnv}

import log.LogLevel

public struct EnvWrapper {
    public operator func [](name: String): Option<String> {
        return getEnv(name)
    }

    public operator func [](name: String, value!: String): Unit {
        return setEnv(name, value)
    }
}

public class Config {
    public static var logLevel = LogLevel.ERROR
    public static var logFile: String = "stderr"

    public static var enableAgentLog = false
    public static var agentLogDir = "./logs/agent-logs"

    public static var saveModelRequest = false
    public static var modelRequestDir = "./logs/model-requests"

    public static var saveCodeInterpreter = false
    public static var codeInterpreterDir = "./logs/code-interpreter-scripts"

    public static var defaultChatModel = Option<ChatModel>.None
    public static var defaultEmbeddingModel = Option<EmbeddingModel>.None

    /**
     * Directory of external scripts, like python scripts
     */
    private static var _externalScriptDir = "./external_scripts"
    public static mut prop externalScriptDir: String {
        get() {
            if (!exists(_externalScriptDir)) {
                throw UnsupportedException("Attempt to access external scripts, but the directory is not set.")
            }
            return _externalScriptDir
        }
        set(p) {
            _externalScriptDir = p
        }
    }

    /**
     * The max retry number when failing to valid get LLM responses
     */
    public static var modelRetryNumber = 3

    /**
     * Whether filter <think> messages generated by reasoning LLMs
     * Only takes effect under synchronous call
     */
    public static var filterThink = false

    /**
     * Provide a way to easily get/set environment variables
     */
    public static let env = EnvWrapper()

    /**
     * The max number of react execution steps
     */
    public static var maxReactNumber = 10

    /**
     * The max retry number when failing to generate outputs of required json schema
     */
    public static var outputRepairRetryNumber = 3

    /**
     * The max connection timeout in http requests. MS
     */
     public static var httpConnectTimeout = 60000 // ms

    /**
     * The max read timeout in http requests. MS
     */
     public static var httpReadTimeout = 60000 // ms
}