package com.placeholder.sherpai2.data.local.entity

import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey
import java.util.UUID

/**
 * Tag type constants - MUST be defined BEFORE TagEntity
 * to avoid KSP initialization order issues
 */
object TagType {
    const val GENERIC = "GENERIC"   // User tags
    const val SYSTEM = "SYSTEM"     // AI/auto tags
    const val HIDDEN = "HIDDEN"     // Internal
}

/**
 * Common system tag values
 */
object SystemTags {
    const val HAS_FACES = "has_faces"
    const val MULTIPLE_PEOPLE = "multiple_people"
    const val LANDSCAPE = "landscape"
    const val PORTRAIT = "portrait"
    const val LOW_QUALITY = "low_quality"
    const val BLURRY = "blurry"
}

/**
 * TagEntity - Normalized tag storage
 *
 * EXPLICIT COLUMN MAPPINGS for KSP compatibility
 */
@Entity(tableName = "tags")
data class TagEntity(

    @PrimaryKey
    @ColumnInfo(name = "tagId")
    val tagId: String,

    @ColumnInfo(name = "type")
    val type: String,

    @ColumnInfo(name = "value")
    val value: String,

    @ColumnInfo(name = "createdAt")
    val createdAt: Long
) {
    companion object {
        /**
         * Create a generic user tag
         */
        fun createUserTag(value: String): TagEntity {
            return TagEntity(
                tagId = UUID.randomUUID().toString(),
                type = TagType.GENERIC,
                value = value.trim().lowercase(),
                createdAt = System.currentTimeMillis()
            )
        }

        /**
         * Create a system tag (auto-generated)
         */
        fun createSystemTag(value: String): TagEntity {
            return TagEntity(
                tagId = UUID.randomUUID().toString(),
                type = TagType.SYSTEM,
                value = value.trim().lowercase(),
                createdAt = System.currentTimeMillis()
            )
        }

        /**
         * Create hidden tag (internal use)
         */
        fun createHiddenTag(value: String): TagEntity {
            return TagEntity(
                tagId = UUID.randomUUID().toString(),
                type = TagType.HIDDEN,
                value = value.trim().lowercase(),
                createdAt = System.currentTimeMillis()
            )
        }
    }

    /**
     * Check if this is a user-created tag
     */
    fun isUserTag(): Boolean = type == TagType.GENERIC

    /**
     * Check if this is a system tag
     */
    fun isSystemTag(): Boolean = type == TagType.SYSTEM

    /**
     * Check if this is a hidden tag
     */
    fun isHiddenTag(): Boolean = type == TagType.HIDDEN

    /**
     * Get display value (capitalized for UI)
     */
    fun getDisplayValue(): String = value.replaceFirstChar { it.uppercase() }
}

/**
 * TagWithUsage - For queries that include usage count
 *
 * NOT AN ENTITY - just a POJO for query results
 * Do NOT add this to @Database entities list!
 */
data class TagWithUsage(
    @ColumnInfo(name = "tagId")
    val tagId: String,

    @ColumnInfo(name = "type")
    val type: String,

    @ColumnInfo(name = "value")
    val value: String,

    @ColumnInfo(name = "createdAt")
    val createdAt: Long,

    @ColumnInfo(name = "usage_count")
    val usageCount: Int
) {
    /**
     * Convert to TagEntity (without usage count)
     */
    fun toTagEntity(): TagEntity {
        return TagEntity(
            tagId = tagId,
            type = type,
            value = value,
            createdAt = createdAt
        )
    }
}