package app.closer.domain.model

import java.time.LocalDate

/**
 * A saved relationship moment visible only to couple members.
 *
 * Memory capsules are created from date ideas, reveals, weekly recaps, or manual entries.
 * Notes are stored encrypted (E2EE). Photos are stored in Firebase Storage and visible only
 * to couple members. Titles and tags are auto-generated from safe context metadata — never
 * from decrypted answer text.
 *
 * @property id Firestore document ID or local ID.
 * @property coupleId The couple that owns this memory.
 * @property authorId User who created the memory.
 * @property title User-set or auto-generated display title.
 * @property date Calendar date associated with the memory.
 * @property category Category key for grouping/filtering (e.g., "date", "reveal", "challenge", "recap", "manual").
 * @property noteCiphertext Optional user-written note, encrypted. Never plaintext.
 * @property photoStoragePath Optional Firebase Storage path for a photo visible only to couple members.
 * @property tags Auto-generated tags from context (category, event type, date).
 * @property source What produced this memory.
 * @property sourceRefId Optional reference to the originating event (date plan ID, question ID, etc.).
 * @property createdAt Unix timestamp of creation.
 * @property updatedAt Unix timestamp of last update.
 */
data class MemoryCapsule(
    val id: String = "",
    val coupleId: String = "",
    val authorId: String = "",
    val title: String = "",
    val date: LocalDate = LocalDate.now(),
    val category: String = "",
    val noteCiphertext: String? = null,
    val photoStoragePath: String? = null,
    val tags: List<String> = emptyList(),
    val source: MemoryCapsuleSource = MemoryCapsuleSource.MANUAL,
    val sourceRefId: String? = null,
    val createdAt: Long = 0L,
    val updatedAt: Long = 0L
)

/**
 * Origin of a memory capsule.
 */
enum class MemoryCapsuleSource {
    DATE_IDEA,
    REVEAL,
    MANUAL,
    WEEKLY_RECAP,
    CHALLENGE;

    fun toFirestoreValue(): String = name.lowercase()

    companion object {
        fun fromFirestoreValue(value: String): MemoryCapsuleSource = when (value.lowercase()) {
            "date_idea", "date" -> DATE_IDEA
            "reveal" -> REVEAL
            "manual" -> MANUAL
            "weekly_recap", "recap" -> WEEKLY_RECAP
            "challenge" -> CHALLENGE
            else -> MANUAL
        }
    }
}

/**
 * A saved date idea ready to become a memory after the date happens.
 *
 * @property dateIdeaId Reference to the saved date idea.
 * @property title Date idea title.
 * @property category Date idea category.
 * @property scheduledDate When the date is planned.
 */
data class MemoryDateIdeaEvent(
    val dateIdeaId: String,
    val title: String = "",
    val category: String = "",
    val scheduledDate: LocalDate? = null
)

/**
 * A reveal ready to become a memory when both partners agree to save it.
 *
 * No decrypted answer text is carried here — only safe metadata.
 *
 * @property questionId Reference to the answered question.
 * @property categoryId Question category.
 * @property revealedDate Date the reveal occurred.
 */
data class MemoryRevealEvent(
    val questionId: String,
    val categoryId: String? = null,
    val revealedDate: LocalDate
)

/**
 * A weekly recap ready to become a memory.
 *
 * @property recapId Reference to the recap document.
 * @property weekStart Start of the recap week.
 * @property weekEnd End of the recap week.
 * @property favoriteCategory Favorite category ID from the recap.
 */
data class MemoryRecapEvent(
    val recapId: String,
    val weekStart: LocalDate,
    val weekEnd: LocalDate,
    val favoriteCategory: String? = null
)

/**
 * A completed challenge ready to become a memory.
 *
 * @property challengeId Reference to the challenge.
 * @property challengeCategory Category ID of the challenge.
 * @property completedDate Date the challenge was completed.
 */
data class MemoryChallengeEvent(
    val challengeId: String,
    val challengeCategory: String? = null,
    val completedDate: LocalDate
)

/**
 * Raw manual input for a memory capsule.
 *
 * @property title User-supplied title.
 * @property date Date associated with the memory.
 * @property category Category key.
 * @property noteCiphertext Encrypted user note.
 * @property photoStoragePath Optional photo path.
 */
data class MemoryManualInput(
    val title: String,
    val date: LocalDate,
    val category: String,
    val noteCiphertext: String? = null,
    val photoStoragePath: String? = null
)
