package de.tsschulz.tt_tagebuch.shared.api.models

import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonElement
import kotlinx.serialization.json.JsonPrimitive
import kotlinx.serialization.json.JsonTransformingSerializer

object MemberDataQualityRequirementsSerializer :
    JsonTransformingSerializer<MemberDataQualityRequirements>(MemberDataQualityRequirements.serializer()) {
    override fun transformDeserialize(element: JsonElement): JsonElement {
        return if (element is JsonPrimitive && element.isString) {
            try {
                Json.parseToJsonElement(element.content)
            } catch (e: Exception) {
                element
            }
        } else {
            element
        }
    }
}

@Serializable
data class MemberDataQualityRequirements(
    val requireStreet: Boolean = true,
    val requirePostalCode: Boolean = true,
    val requireCity: Boolean = true,
    val requirePhone: Boolean = true,
    val requireEmail: Boolean = true,
)

@Serializable
data class UpdateClubSettingsBody(
    val greetingText: String? = null,
    val associationMemberNumber: String? = null,
    val countryCode: String? = null,
    val stateCode: String? = null,
    val myTischtennisFedNickname: String? = null,
    val autoFetchRankings: Boolean? = null,
    @Serializable(with = MemberDataQualityRequirementsSerializer::class)
    val memberDataQualityRequirements: MemberDataQualityRequirements? = null,
)

@Serializable
data class Club(
    val id: Int,
    val name: String,
    val greetingText: String? = null,
    val associationMemberNumber: String? = null,
    val myTischtennisFedNickname: String? = null,
    val autoFetchRankings: Boolean? = null,
    val countryCode: String? = null,
    val stateCode: String? = null,
    @Serializable(with = MemberDataQualityRequirementsSerializer::class)
    val memberDataQualityRequirements: MemberDataQualityRequirements? = null,
)

