package com.correx.core.events.events

import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue

class ClarificationQuestionsTest {

    @Test
    fun `parses structured questions with options and multiSelect`() {
        val json = """
            {
              "summary": "build a UI",
              "questions": [
                {"id": "stack", "prompt": "Which frontend stack?", "header": "Stack",
                 "options": ["React", "Vue", "Svelte"]},
                {"id": "shape", "prompt": "SPA or multi-page?", "multiSelect": true,
                 "options": ["SPA", "MPA"]}
              ]
            }
        """.trimIndent()
        val questions = ClarificationQuestions.parse(json)
        assertEquals(2, questions.size)
        assertEquals("Which frontend stack?", questions[0].prompt)
        assertEquals(listOf("React", "Vue", "Svelte"), questions[0].options)
        assertEquals("Stack", questions[0].header)
        assertTrue(questions[1].multiSelect)
    }

    @Test
    fun `accepts question alias and synthesises ids, defaults free-text`() {
        val json = """{"questions": [{"question": "What deadline?"}]}"""
        val questions = ClarificationQuestions.parse(json)
        assertEquals(1, questions.size)
        assertEquals("What deadline?", questions[0].prompt)
        assertEquals("q1", questions[0].id)
        assertTrue(questions[0].options.isEmpty(), "no options → free-text answer")
        assertEquals(false, questions[0].multiSelect)
    }

    @Test
    fun `tolerates a markdown code fence around the artifact`() {
        val fenced = "```json\n{\"questions\": [{\"prompt\": \"Q?\"}]}\n```"
        assertEquals(1, ClarificationQuestions.parse(fenced).size)
    }

    @Test
    fun `absent or empty questions array yields no clarification`() {
        assertTrue(ClarificationQuestions.parse("""{"summary": "no questions here"}""").isEmpty())
        assertTrue(ClarificationQuestions.parse("""{"questions": []}""").isEmpty())
    }

    @Test
    fun `malformed json yields empty rather than throwing`() {
        assertTrue(ClarificationQuestions.parse("not json at all").isEmpty())
        assertTrue(ClarificationQuestions.parse("").isEmpty())
    }

    @Test
    fun `skips elements lacking a prompt`() {
        val json = """{"questions": [{"id": "x"}, {"prompt": "real?"}]}"""
        val questions = ClarificationQuestions.parse(json)
        assertEquals(1, questions.size)
        assertEquals("real?", questions[0].prompt)
    }
}
