package com.atridad.ascently.data.model

import androidx.compose.runtime.Immutable
import androidx.room.Entity
import androidx.room.ForeignKey
import androidx.room.Index
import androidx.room.PrimaryKey
import com.atridad.ascently.utils.DateFormatUtils
import kotlinx.serialization.Serializable

@Immutable
@Entity(
    tableName = "problems",
    foreignKeys =
    [
        ForeignKey(
            entity = Gym::class,
            parentColumns = ["id"],
            childColumns = ["gymId"],
            onDelete = ForeignKey.CASCADE,
        ),
    ],
    indices = [Index(value = ["gymId"])],
)
@Serializable
data class Problem(
    @PrimaryKey val id: String,
    val gymId: String,
    val name: String? = null,
    val description: String? = null,
    val climbType: ClimbType,
    val difficulty: DifficultyGrade,
    val tags: List<String> = emptyList(),
    val location: String? = null,
    val imagePaths: List<String> = emptyList(),
    val isActive: Boolean = true,
    val dateSet: String? = null,
    val notes: String? = null,
    val createdAt: String,
    val updatedAt: String,
) {
    companion object {
        fun create(
            gymId: String,
            name: String? = null,
            description: String? = null,
            climbType: ClimbType,
            difficulty: DifficultyGrade,
            tags: List<String> = emptyList(),
            location: String? = null,
            imagePaths: List<String> = emptyList(),
            dateSet: String? = null,
            notes: String? = null,
        ): Problem {
            val now = DateFormatUtils.nowISO8601()
            return Problem(
                id = java.util.UUID.randomUUID().toString(),
                gymId = gymId,
                name = name,
                description = description,
                climbType = climbType,
                difficulty = difficulty,
                tags = tags,
                location = location,
                imagePaths = imagePaths,
                isActive = true,
                dateSet = dateSet,
                notes = notes,
                createdAt = now,
                updatedAt = now,
            )
        }
    }
}
