package com.chick_mood.data.model

import androidx.room.Entity
import androidx.room.PrimaryKey
import androidx.room.ColumnInfo

/**
 * 用户配置键值对数据模型
 * 使用键值对方式存储用户的各种配置信息
 * 支持灵活的配置管理和扩展
 *
 * @author Claude
 * @date 2025-10-22
 */
@Entity(tableName = "user_config_entries")
data class UserConfigEntry(
    /**
     * 主键ID，自动生成
     */
    @PrimaryKey(autoGenerate = true)
    val id: Long = 0,

    /**
     * 配置键名
     */
    @ColumnInfo(name = "config_key")
    val configKey: String,

    /**
     * 配置值
     */
    @ColumnInfo(name = "config_value")
    val configValue: String,

    /**
     * 配置更新时间
     */
    @ColumnInfo(name = "updated_at")
    val updatedAt: Long = System.currentTimeMillis()
) {
    companion object {
        // 配置键常量
        const val KEY_USER_NICKNAME = "user_nickname"
        const val KEY_USER_AVATAR = "user_avatar"
        const val KEY_APP_VERSION = "app_version"
        const val KEY_FIRST_USE_TIME = "first_use_time"
        const val KEY_USAGE_DAYS = "usage_days"
        const val KEY_TOTAL_MOOD_RECORDS = "total_mood_records"
        const val KEY_LAST_ACTIVE_TIME = "last_active_time"
        const val KEY_MORNING_REMINDER = "morning_reminder"
        const val KEY_EVENING_REMINDER = "evening_reminder"
        const val KEY_NOTIFICATION_ENABLED = "notification_enabled"
        const val KEY_SOUND_ENABLED = "sound_enabled"
        const val KEY_VIBRATION_ENABLED = "vibration_enabled"
        const val KEY_DARK_MODE = "dark_mode"
    }

    /**
     * 检查配置是否有效
     */
    fun isValid(): Boolean {
        return configKey.isNotEmpty() &&
               configValue.isNotEmpty() &&
               updatedAt > 0
    }

    /**
     * 检查是否为布尔类型配置
     */
    fun isBooleanConfig(): Boolean {
        return configKey.endsWith("_enabled") ||
               configKey.startsWith("is_") ||
               configKey.contains("flag")
    }

    /**
     * 获取布尔值
     */
    fun getBooleanValue(): Boolean {
        return configValue.equals("1", ignoreCase = true) ||
               configValue.equals("true", ignoreCase = true)
    }

    /**
     * 检查是否为时间配置
     */
    fun isTimeConfig(): Boolean {
        return configKey.endsWith("_reminder") ||
               configKey.endsWith("_time") ||
               configValue.matches(Regex("^([0-1]?[0-9]|2[0-3]):[0-5][0-9]$"))
    }

    /**
     * 检查是否为数字配置
     */
    fun isNumericConfig(): Boolean {
        return configKey.endsWith("_days") ||
               configKey.endsWith("_count") ||
               configKey.endsWith("_total") ||
               configValue.matches(Regex("^\\d+$"))
    }

    /**
     * 获取整数值
     */
    fun getIntValue(): Int {
        return try {
            configValue.toInt()
        } catch (e: NumberFormatException) {
            0
        }
    }

    /**
     * 获取长整数值
     */
    fun getLongValue(): Long {
        return try {
            configValue.toLong()
        } catch (e: NumberFormatException) {
            0L
        }
    }

    /**
     * 获取配置类型描述
     */
    fun getConfigTypeDescription(): String {
        return when {
            isBooleanConfig() -> "布尔配置"
            isTimeConfig() -> "时间配置"
            isNumericConfig() -> "数字配置"
            else -> "文本配置"
        }
    }

    /**
     * 获取格式化的配置值
     */
    fun getFormattedValue(): String {
        return when {
            isBooleanConfig() -> if (getBooleanValue()) "启用" else "禁用"
            isTimeConfig() -> configValue
            isNumericConfig() -> configValue
            configKey == KEY_USER_NICKNAME && configValue.isNotEmpty() -> configValue
            configKey == KEY_APP_VERSION -> "v$configValue"
            else -> configValue
        }
    }

    }