package com.tomhempel.rbarometer.questionnaire

data class Option(
    val key: String,               // Must always be set
    val nextQuestionId: String? = null
)

data class ValueOption(
    val value: Int,
    val nextQuestionId: String? = null
)

data class Range(
    val min: Int,
    val max: Int
)

data class GlassSymptomLabel(
    val key: String = "",
    val labelDe: String = "",
)

data class Constraints(
    val notBefore: String? = null,
    val notAfter: String? = null
)

sealed class QuestionItem {
    abstract val layout: String?
    abstract val id: String

    data class ClientCoachCodeQuestion(
        override val layout: String?,
        override val id: String,
        val question: String,
        val hint1: String? = null,
        val hint2: String? = null,
        val nextQuestionId: String? = null,
    ) : QuestionItem()

    data class RadioQuestion(
        override val layout: String?,
        override val id: String,
        val textKey: String?,
        val question: String?,
        val options: List<Option> = emptyList(),
        val pointsMap: Map<String, Int>? = null,
        val noteBeforeKey: String? = null,
        val noteAfterKey: String? = null,
    ) : QuestionItem()

    data class GlassScaleQuestion(
        override val layout: String?,
        override val id: String,
        val textKey: String?,
        val question: String,
        val symptomTextKeys: List<String> = emptyList(),
        val glassSymptoms: List<GlassSymptomLabel> = emptyList(),
        val pointsMap: Map<String, Int> = emptyMap(),
        val symptoms: List<String> = emptyList(),
        /** glass (default) | thermometer */
        val scaleType: String? = null,
        val nextQuestionId: String? = null,
    ) : QuestionItem()

    data class MultiCheckboxQuestion(
        override val id: String,
        override val layout: String,
        val textKey: String?,
        val question: String,
        val options: List<Option> = emptyList(),
        val nextQuestionId: String? = null,
        val otherOptionKey: String? = null,
        val otherNextQuestionId: String? = null,
        val pointsMap: Map<String, Int>? = null,
        val minSelection: Int = 1
    ) : QuestionItem()

    data class DateSpinnerQuestion(
        override val layout: String?,
        override val id: String,
        val textKey: String?,
        val question: String,
        val nextQuestionId: String? = null,
        val storeAsGlobalKey: String? = null,
        val constraints: Constraints? = null,
        /** year | year_month | full (default) */
        val precision: String? = null,
        override val noteBeforeKey: String? = null,
        override val noteAfterKey: String? = null,
    ) : QuestionItem(), QuestionWithNotes

    data class ValueSpinnerQuestion(
        override val layout: String?,
        override val id: String,
        val textKey: String?,
        val question: String,
        val options: List<ValueOption> = emptyList(),
        val range: Range? = null,
        val nextQuestionId: String? = null,
        val noteBeforeKey: String? = null,
        val noteAfterKey: String? = null,
    ) : QuestionItem()

    data class SliderQuestion(
        override val layout: String?,
        override val id: String,
        val textKey: String?,
        val question: String,
        val range: Range? = null,
        val step: Int? = null,
        val unitLabel: String? = null,
        val nextQuestionId: String? = null,
        val noteBeforeKey: String? = null,
        val noteAfterKey: String? = null,
    ) : QuestionItem()

    data class StringSpinnerQuestion(
        override val layout: String?,
        override val id: String,
        val textKey: String?,
        val question: String,
        val options: List<String> = emptyList(),
        val nextQuestionId: String? = null,
        val otherNextQuestionId: String? = null,
        val otherOptionKey: String? = null,
    ) : QuestionItem()

    data class FreeTextQuestion(
        override val layout: String?,
        override val id: String,
        val textKey: String? = null,
        val question: String,
        val hint: String? = null,
        val maxLength: Int = 500,
        val nextQuestionId: String? = null,
        val noteBeforeKey: String? = null,
        val noteAfterKey: String? = null,
    ) : QuestionItem()

    data class LastPage(
        override val layout: String?,
        override val id: String,
        val textKey: String,
        val question: String
    ) : QuestionItem()

    data class QuestionnaireEntry(
        val file: String,
        val condition: Condition? = null,
        val showPoints: Boolean = false,
        val serverName: String? = null,
        val categoryKey: String? = null,
        /** Translation key shown when this questionnaire is locked (conditionJson.messageKey). */
        val messageKey: String? = null,
    )

    // flexible Condition-Typen für die questionnaire_order.json
    sealed class Condition {
        object AlwaysAvailable : Condition()
        data class RequiresCompleted(val required: List<String>) : Condition()
        data class QuestionCondition(
            val questionnaire: String,
            val questionId: String,
            val operator: String,
            val value: String
        ) : Condition()

        data class Combined(
            val requiresCompleted: List<String>?,
            val questionCheck: QuestionCondition?
        ) : Condition()

        data class AnyOf(val conditions: List<Condition>) : Condition()
    }
}
