package com.correx.core.config

import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
import java.nio.file.Files
import java.nio.file.Paths

class ProjectProfileLoaderTest {

    private fun tempRoot(): String {
        val dir = Files.createTempDirectory("correx-project-profile-test")
        Files.createDirectories(dir.resolve(".correx"))
        return dir.toString()
    }

    @Test
    fun `missing file returns default ProjectProfile`() {
        assertEquals(ProjectProfile(), ProjectProfileLoader.load(tempRoot()))
    }

    @Test
    fun `full file populates about conventions and commands`() {
        val root = tempRoot()
        Files.writeString(
            Paths.get(root, ".correx", "project.toml"),
            """
            about = "Event-sourced orchestration kernel, Kotlin/JVM 21"
            conventions = ["no bare try-catch", "events are the only source of truth"]

            [commands]
            build = "./gradlew build"
            test = "./gradlew check"
            """.trimIndent(),
        )
        val p = ProjectProfileLoader.load(root)
        assertEquals("Event-sourced orchestration kernel, Kotlin/JVM 21", p.about)
        assertEquals(listOf("no bare try-catch", "events are the only source of truth"), p.conventions)
        assertEquals(mapOf("build" to "./gradlew build", "test" to "./gradlew check"), p.commands)
    }

    @Test
    fun `multi-line conventions array parses all entries`() {
        // Idiomatic TOML splits long string arrays across lines; the line-based parser used
        // to swallow everything after the opening bracket (conventions became listOf("[")).
        val root = tempRoot()
        Files.writeString(
            Paths.get(root, ".correx", "project.toml"),
            """
            about = "kernel"
            conventions = [
                "no bare try-catch",
                # comment inside the array
                "events are the only source of truth",
            ]

            [commands]
            test = "./gradlew check"
            """.trimIndent(),
        )
        val p = ProjectProfileLoader.load(root)
        assertEquals(listOf("no bare try-catch", "events are the only source of truth"), p.conventions)
        assertEquals(mapOf("test" to "./gradlew check"), p.commands)
    }

    @Test
    fun `toolchain command tables are represented as dotted command keys`() {
        val root = tempRoot()
        Files.writeString(
            Paths.get(root, ".correx", "project.toml"),
            """
            [commands]
            build = "./gradlew assemble"

            [commands.node]
            build = "npm --prefix frontend run build"
            test = "npm --prefix frontend test"
            """.trimIndent(),
        )

        assertEquals(
            mapOf(
                "build" to "./gradlew assemble",
                "node.build" to "npm --prefix frontend run build",
                "node.test" to "npm --prefix frontend test",
            ),
            ProjectProfileLoader.load(root).commands,
        )
    }

    @Test
    fun `malformed file returns default without throwing`() {
        val root = tempRoot()
        Files.writeString(Paths.get(root, ".correx", "project.toml"), "[[[not toml")
        assertEquals(ProjectProfile(), ProjectProfileLoader.load(root))
    }
}
