package com.correx.core.config

import org.junit.jupiter.api.Test
import kotlin.test.assertEquals

class CorrexConfigWriterTest {

    @Test
    fun `round-trips defaults`() {
        val cfg = CorrexConfig()
        assertEquals(cfg, ConfigLoader.parseTomlForTest(CorrexConfigWriter.write(cfg)))
    }

    @Test
    fun `round-trips a populated config`() {
        val cfg = CorrexConfig(
            server = ServerConfig(host = "0.0.0.0", port = 9090),
            tui = TuiConfig(theme = "soft-blue", sessionListLimit = 9),
            cli = CliConfig(defaultOutput = "json"),
            tools = ToolsConfig(
                sandboxRoot = "/tmp/sb",
                workingDir = "/work",
                workspaceRoot = "/ws",
                shellEnabled = false,
                fileEditEnabled = false,
                shellAllowedExecutables = listOf("git", "ls"),
                networkAllowedHosts = listOf("example.com"),
            ),
            talkie = TalkieConfig(
                conversationKeepLast = 7,
                retrievalK = 4,
                tokenBudget = 5000,
                embedder = EmbedderConfig(backend = "remote", dimension = 768, url = "http://e", modelId = "emb"),
                l3 = L3Config(backend = "turbovec", persistPath = "/l3", dim = 768, bitWidth = 8),
                generation = GenerationSettings(temperature = 0.3, topP = 0.8, maxTokens = 1024),
                narration = NarrationSettings(temperature = 0.1, topP = 0.95, maxTokens = 256, maxPerRun = 3),
            ),
            personalization = PersonalizationConfig(enabled = true, learn = true),
            project = ProjectConfig(enabled = true, memoryK = 8, maxDepth = 6, injectTopK = 40),
            git = GitConfig(enabled = true, remote = "gitea", baseBranch = "develop", author = "Correx <bot@example.test>"),
            modelsSettings = ModelsSettings(defaultModel = "m1", host = "0.0.0.0", port = 10001),
            orchestration = OrchestrationKnobs(stageTimeoutMs = 90_000, journalCompactionTokenThreshold = 12_000),
            providers = listOf(
                ProviderConfig(
                    id = "p1", type = "llamacpp", modelId = "x", url = "http://127.0.0.1:10000",
                    capabilities = mapOf("code" to 0.9),
                ),
            ),
            models = listOf(
                ModelConfig(id = "m1", modelPath = "/m1.gguf", contextSize = 4096, capabilities = mapOf("chat" to 0.8)),
            ),
            artifacts = listOf(
                ArtifactKindConfig(id = "review", schemaPath = "schemas/review.json", llmEmitted = true),
            ),
        )
        assertEquals(cfg, ConfigLoader.parseTomlForTest(CorrexConfigWriter.write(cfg)))
    }
}
