package com.tomhempel.rbarometer.questionnaire

/**
 * Stable keys for Room answer rows and submit mapping.
 */
object AnswerKeyUtils {

    /** Room `questions.questionId` / `answers.questionId` format. */
    fun roomQuestionId(questionnaireId: String, answerKey: String): String =
        "$questionnaireId-$answerKey"

    /**
     * Lookup key inside a per-client answer map.
     * Accepts full room ids (`questionnaireId-key`) or short ids.
     */
    fun lookupKey(questionnaireId: String, storedQuestionId: String): String {
        val prefix = "$questionnaireId-"
        return if (storedQuestionId.startsWith(prefix)) {
            storedQuestionId.removePrefix(prefix)
        } else {
            storedQuestionId.substringAfterLast('-')
        }
    }
}
