package com.tomhempel.rbarometer.data

import androidx.room.*

@Entity(tableName = "clients")
data class Client(
    @PrimaryKey val clientCodeKey: String,
    val clientCodeEnc: String,
)

@Entity(tableName = "questionnaires")
data class Questionnaire(
    @PrimaryKey val id: String,
)

@Entity(
    tableName = "questions",
    foreignKeys = [
        ForeignKey(
            entity = Questionnaire::class,
            parentColumns = ["id"],
            childColumns = ["questionnaireId"],
            onDelete = ForeignKey.CASCADE,
        )
    ],
    indices = [Index("questionnaireId")],
)
data class Question(
    @PrimaryKey val questionId: String,
    val questionnaireId: String,
    val question: String = "",
)

@Entity(
    tableName = "answers",
    primaryKeys = ["clientCodeKey", "questionId"],
    foreignKeys = [
        ForeignKey(
            entity = Client::class,
            parentColumns = ["clientCodeKey"],
            childColumns = ["clientCodeKey"],
            onDelete = ForeignKey.CASCADE,
        ),
        ForeignKey(
            entity = Question::class,
            parentColumns = ["questionId"],
            childColumns = ["questionId"],
            onDelete = ForeignKey.CASCADE,
        ),
    ],
    indices = [Index("clientCodeKey"), Index("questionId")],
)
data class AnswerEntity(
    val clientCodeKey: String,
    val questionId: String,
    val answerValueEnc: String = "",
)

/** Plaintext answer row used by app code (never persisted without encryption). */
data class Answer(
    val clientCode: String,
    val questionId: String,
    val answerValue: String = "",
)

@Entity(
    tableName = "completed_questionnaires",
    primaryKeys = ["clientCodeKey", "questionnaireId"],
    foreignKeys = [
        ForeignKey(
            entity = Client::class,
            parentColumns = ["clientCodeKey"],
            childColumns = ["clientCodeKey"],
            onDelete = ForeignKey.CASCADE,
        ),
        ForeignKey(
            entity = Questionnaire::class,
            parentColumns = ["id"],
            childColumns = ["questionnaireId"],
            onDelete = ForeignKey.CASCADE,
        ),
    ],
    indices = [Index("clientCodeKey"), Index("questionnaireId")],
)
data class CompletedQuestionnaireEntity(
    val clientCodeKey: String,
    val questionnaireId: String,
    val timestamp: Long = System.currentTimeMillis(),
    val isDone: Boolean,
    val sumPoints: Int? = null,
    val uploadedAt: Long? = null,
)

data class CompletedQuestionnaire(
    val clientCode: String,
    val questionnaireId: String,
    val timestamp: Long = System.currentTimeMillis(),
    val isDone: Boolean,
    val sumPoints: Int? = null,
    val uploadedAt: Long? = null,
)

@Entity(
    tableName = "coach_scoring_reviews",
    primaryKeys = ["clientCodeKey", "profileID"],
    foreignKeys = [
        ForeignKey(
            entity = Client::class,
            parentColumns = ["clientCodeKey"],
            childColumns = ["clientCodeKey"],
            onDelete = ForeignKey.CASCADE,
        ),
    ],
    indices = [Index("clientCodeKey")],
)
data class CoachScoringReviewEntity(
    val clientCodeKey: String,
    val profileID: String,
    val profileName: String = "",
    val coachBand: String,
    val calculatedBand: String,
    val weightedTotal: Double,
    val updatedAt: Long = System.currentTimeMillis(),
    val uploadedAt: Long? = null,
)

data class CoachScoringReview(
    val clientCode: String,
    val profileID: String,
    val profileName: String = "",
    val coachBand: String,
    val calculatedBand: String,
    val weightedTotal: Double,
    val updatedAt: Long = System.currentTimeMillis(),
    val uploadedAt: Long? = null,
) {
    val isPendingUpload: Boolean
        get() = uploadedAt == null || uploadedAt < updatedAt
}
