package com.correx.core.talkie

import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Test

class IdeasTest {

    @Test
    fun `parses a fenced ideas block and strips it from the prose`() {
        val content = """
            Good point — two things worth remembering.

            ```json
            {"ideas":["cache the repo map across sessions","add a --dry-run flag"]}
            ```
        """.trimIndent()

        val parsed = Ideas.parse(content)

        assertEquals(listOf("cache the repo map across sessions", "add a --dry-run flag"), parsed.ideas)
        assertTrue(parsed.cleanText.startsWith("Good point"))
        assertTrue("```" !in parsed.cleanText, "fence must be stripped from the visible turn")
    }

    @Test
    fun `accepts object items with a text field`() {
        val content = "prose\n```json\n{\"ideas\":[{\"text\":\"idea one\"},{\"idea\":\"idea two\"}]}\n```"
        val parsed = Ideas.parse(content)
        assertEquals(listOf("idea one", "idea two"), parsed.ideas)
    }

    @Test
    fun `finds the ideas block even when it is not the first fence`() {
        val content = "```json\n{\"propose_workflows\":[{\"id\":\"x\"}]}\n```\n" +
            "and\n```json\n{\"ideas\":[\"keep this\"]}\n```"
        val parsed = Ideas.parse(content)
        assertEquals(listOf("keep this"), parsed.ideas)
    }

    @Test
    fun `no block yields no ideas and untouched text`() {
        val content = "Let's just talk it through."
        val parsed = Ideas.parse(content)
        assertTrue(parsed.ideas.isEmpty())
        assertEquals(content, parsed.cleanText)
    }

    @Test
    fun `blank and empty items are dropped`() {
        val content = "x\n```json\n{\"ideas\":[\"  \",\"real\",\"\"]}\n```"
        val parsed = Ideas.parse(content)
        assertEquals(listOf("real"), parsed.ideas)
    }
}
