package config

import (
	"os"
	"strconv"
)

type Config struct {
	AppEnv                          string
	HTTPAddr                        string
	DatabaseURL                     string
	RedisURL                        string
	RedisKeyPrefix                  string
	AuthUsername                    string
	AuthPassword                    string
	ModelBaseURL                    string
	ModelAPIKey                     string
	ModelName                       string
	ModelContextSize                int
	ModelTimeoutSec                 int
	A2AEdgeURL                      string
	A2AAgentURL                     string
	A2AToken                        string
	A2ATimeoutSec                   int
	MattermostBaseURL               string
	MattermostToken                 string
	PlaneBaseURL                    string
	PlaneToken                      string
	WorkflowTaskTimeoutSec          int
	ProtoSocketPath                 string
	ProtoSocketHeartbeatIntervalSec int
	ProtoSocketHeartbeatWaitSec     int
}

func Load() Config {
	return Config{
		AppEnv:                          getEnv("APP_ENV", "local"),
		HTTPAddr:                        getEnv("HTTP_ADDR", ":8080"),
		DatabaseURL:                     os.Getenv("DATABASE_URL"),
		RedisURL:                        os.Getenv("REDIS_URL"),
		RedisKeyPrefix:                  os.Getenv("REDIS_KEY_PREFIX"),
		AuthUsername:                    getEnv("AUTH_USERNAME", "nomadcode"),
		AuthPassword:                    os.Getenv("AUTH_PASSWORD"),
		ModelBaseURL:                    getEnv("MODEL_BASE_URL", "http://192.168.0.91:11434"),
		ModelAPIKey:                     getEnv("MODEL_API_KEY", "ollama"),
		ModelName:                       getEnv("MODEL_NAME", "qwen3.6:35b-a3b-bf16"),
		ModelContextSize:                getEnvInt("MODEL_CONTEXT_SIZE", 262144),
		ModelTimeoutSec:                 getEnvInt("MODEL_TIMEOUT_SEC", 300),
		A2AEdgeURL:                      firstEnv("A2A_EDGE_URL", "A2A_AGENT_URL"),
		A2AAgentURL:                     firstEnv("A2A_AGENT_URL", "A2A_EDGE_URL"),
		A2AToken:                        os.Getenv("A2A_TOKEN"),
		A2ATimeoutSec:                   getEnvInt("A2A_TIMEOUT_SEC", 300),
		MattermostBaseURL:               os.Getenv("MATTERMOST_BASE_URL"),
		MattermostToken:                 os.Getenv("MATTERMOST_TOKEN"),
		PlaneBaseURL:                    os.Getenv("PLANE_BASE_URL"),
		PlaneToken:                      os.Getenv("PLANE_TOKEN"),
		WorkflowTaskTimeoutSec:          getEnvInt("WORKFLOW_TASK_TIMEOUT_SEC", 300),
		ProtoSocketPath:                 getEnv("PROTO_SOCKET_PATH", "/proto-socket"),
		ProtoSocketHeartbeatIntervalSec: getEnvInt("PROTO_SOCKET_HEARTBEAT_INTERVAL_SEC", 30),
		ProtoSocketHeartbeatWaitSec:     getEnvInt("PROTO_SOCKET_HEARTBEAT_WAIT_SEC", 10),
	}
}

func getEnv(key, fallback string) string {
	value := os.Getenv(key)
	if value == "" {
		return fallback
	}
	return value
}

func firstEnv(keys ...string) string {
	for _, key := range keys {
		if value := os.Getenv(key); value != "" {
			return value
		}
	}
	return ""
}

func getEnvInt(key string, fallback int) int {
	value := os.Getenv(key)
	if value == "" {
		return fallback
	}
	parsed, err := strconv.Atoi(value)
	if err != nil {
		return fallback
	}
	return parsed
}
