@file:Suppress("HardCodedStringLiteral")

package eu.gaudian.translator.utils

class PromptBuilder(var basePrompt: String) {

    private val details = mutableListOf<String>()
    private var jsonStructure: String? = null
    private var example: String? = null

    fun addDetail(detail: String): PromptBuilder {
        details.add(detail)
        return this
    }

    fun withJsonResponse(description: String): PromptBuilder {
        this.jsonStructure = "Return strictly valid JSON (RFC 8259): double-quoted keys and string values, no comments, no trailing commas, no markdown fences: $description."
        return this
    }

    fun withExample(originalText: String): PromptBuilder {
        this.example = "Original Text: \"$originalText\""
        return this
    }

    fun build(): String {
        var prompt = basePrompt
        if (details.isNotEmpty()) {
            prompt += " " + details.joinToString(" ")
        }
        jsonStructure?.let {
            prompt += " $it"
        }
        example?.let {
            prompt += "\n\n$it"
        }
        // Ensures the prompt is clean and compact.
        return prompt.trimIndent().replace(Regex("\\s+"), " ")
    }
}