package com.tomhempel.rbarometer.network

data class ApiError(
    val code: String? = null,
    val message: String = ""
)

data class LoginResult(
    val token: String,
    val user: String,
    val role: String? = null,
    val mustChangePassword: Boolean = false,
    val assignedClientCodes: List<String> = emptyList()
)

data class SessionInfo(
    val valid: Boolean,
    val user: String = "",
    val role: String = "",
    val mustChangePassword: Boolean = false,
)

data class QuestionnaireListItem(
    val id: String,
    val name: String,
    val showPoints: Boolean = false,
    /** Raw JSON from API `condition`; cached for offline availability rules. */
    val conditionJson: String = "{}",
    /** Optional opening-screen group key (label via app UI strings). */
    val categoryKey: String? = null,
    /** Server structure revision; bump when questions/options change structurally. */
    val structureRevision: Int = 1,
)

/** Server payload for one completed questionnaire (GET ?answers=1). */
data class ServerQuestionnaireAnswers(
    val questionnaireID: String,
    val completedAt: Long? = null,
    val sumPoints: Int = 0,
    val answers: List<SubmitAnswer> = emptyList(),
)

data class ClientAnswersBundle(
    val clientCode: String,
    val questionnaires: List<ServerQuestionnaireAnswers> = emptyList(),
)

data class SubmitQuestionnaireRequest(
    val questionnaireID: String,
    val clientCode: String,
    val startedAt: Long? = null,
    val completedAt: Long? = null,
    val structureRevision: Int? = null,
    val answers: List<SubmitAnswer>
)

data class SubmitAnswer(
    val questionID: String,
    val answerOptionKey: String? = null,
    val freeTextValue: String? = null,
    val numericValue: Double? = null,
    val answeredAt: Long? = null
)

data class SubmitQuestionnaireResponse(
    val submitted: Boolean = false,
    val sumPoints: Int = 0,
    val structureRevision: Int? = null,
    val legacySubmit: Boolean = false,
    val cycleAdvanced: Boolean = false,
    val cycleNumber: Int? = null,
)

data class ProgramSessionQuestionnaire(
    val questionnaireID: String,
    val orderIndex: Int = 0,
)

data class ProgramSessionDefinition(
    val sessionID: String,
    val name: String,
    val description: String = "",
    val sessionNumber: Int = 0,
    val orderIndex: Int = 0,
    val questionnaires: List<ProgramSessionQuestionnaire> = emptyList(),
)

data class ProgramSessionSettings(
    val autoRepeatSessions: Boolean = false,
)

data class ProgramSessionsPayload(
    val sessions: List<ProgramSessionDefinition> = emptyList(),
    val settings: ProgramSessionSettings = ProgramSessionSettings(),
)

data class ProgramProgress(
    val cycleNumber: Int = 1,
    val cycleStartedAt: Long = 0L,
    val totalSessions: Int = 0,
    val sessionsCompletedInCycle: Int = 0,
    val currentSessionID: String? = null,
    val currentSessionNumber: Int? = null,
    val currentSessionName: String? = null,
    val cycleComplete: Boolean = false,
    val programComplete: Boolean = false,
    val autoRepeatSessions: Boolean = false,
)

/** One questionnaire already completed on the server side. */
data class ServerCompletedQuestionnaire(
    val questionnaireID: String,
    val sumPoints: Int = 0,
    /** Unix seconds (as returned by server). */
    val completedAt: Long = 0L
)

/** A client with its server-side completion records. */
data class AssignedClientInfo(
    val clientCode: String,
    val completedQuestionnaires: List<ServerCompletedQuestionnaire> = emptyList(),
    val programProgress: ProgramProgress? = null,
)

data class ScoringProfileMember(
    val questionnaireID: String,
    val weight: Double = 1.0,
    val orderIndex: Int = 0,
    val name: String = "",
)

data class ScoringProfileDefinition(
    val profileID: String,
    val name: String,
    val description: String = "",
    val isActive: Boolean = true,
    val greenMin: Int = 0,
    val greenMax: Int = 12,
    val yellowMin: Int = 13,
    val yellowMax: Int = 36,
    val redMin: Int = 37,
    val questionnaires: List<ScoringProfileMember> = emptyList(),
)

data class ScoringReviewProfile(
    val profileID: String,
    val name: String,
    val weightedTotal: Double = 0.0,
    val calculatedBand: String = "",
    val greenMin: Int = 0,
    val greenMax: Int = 12,
    val yellowMin: Int = 13,
    val yellowMax: Int = 36,
    val redMin: Int = 37,
    val coachBand: String? = null,
    val pendingReview: Boolean = true,
    /** Coach decision saved on device but not uploaded yet. */
    val pendingUpload: Boolean = false,
)

data class ClientScoringReview(
    val clientCode: String,
    val profiles: List<ScoringReviewProfile> = emptyList(),
)

data class CoachScoringReviewRequest(
    val action: String = "coachScoringReview",
    val clientCode: String,
    val profileID: String,
    val coachBand: String,
    val calculatedBand: String,
    val weightedTotal: Double,
)

class ApiException(
    message: String,
    val code: String? = null,
    /** Server `error.details` (e.g. validation errors list). */
    val details: com.google.gson.JsonElement? = null,
) : Exception(message)
