package com.atridad.openclimb.data.model

import androidx.room.Entity
import androidx.room.ForeignKey
import androidx.room.Index
import androidx.room.PrimaryKey
import kotlinx.serialization.Serializable
import java.time.LocalDateTime

@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 setter: String? = null, // Route setter name
    val tags: List<String> = emptyList(), // e.g., "overhang", "slab", "crimpy"
    val location: String? = null, // Wall section, area in gym
    val imagePaths: List<String> = emptyList(), // Local file paths to photos
    val isActive: Boolean = true, // Whether the problem is still up
    val dateSet: String? = null, // When the problem was set
    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,
            setter: String? = null,
            tags: List<String> = emptyList(),
            location: String? = null,
            imagePaths: List<String> = emptyList(),
            dateSet: String? = null,
            notes: String? = null
        ): Problem {
            val now = LocalDateTime.now().toString()
            return Problem(
                id = java.util.UUID.randomUUID().toString(),
                gymId = gymId,
                name = name,
                description = description,
                climbType = climbType,
                difficulty = difficulty,
                setter = setter,
                tags = tags,
                location = location,
                imagePaths = imagePaths,
                isActive = true,
                dateSet = dateSet,
                notes = notes,
                createdAt = now,
                updatedAt = now
            )
        }
    }
}
