package com.chick_mood.data.model

import org.junit.Assert.*
import org.junit.Test

/**
 * UserConfig数据模型的单元测试
 * 测试用户配置数据模型的各种功能
 *
 * @author Claude
 * @date 2025-10-22
 */
class UserConfigTest {

    /**
     * 测试用户配置的基本属性和初始化
     */
    @Test
    fun testUserConfigBasicProperties() {
        val config = UserConfig(
            id = 1,
            nickname = "测试用户",
            avatarPath = "/path/to/avatar.jpg",
            isDarkMode = true,
            isSoundEnabled = false,
            isVibrationEnabled = false,
            isReminderEnabled = true,
            reminderHour = 21,
            reminderMinute = 30,
            appVersion = "1.0.1",
            dataVersion = 2,
            totalUsageCount = 15
        )

        assertEquals(1, config.id)
        assertEquals("测试用户", config.nickname)
        assertEquals("/path/to/avatar.jpg", config.avatarPath)
        assertTrue(config.isDarkMode)
        assertFalse(config.isSoundEnabled)
        assertFalse(config.isVibrationEnabled)
        assertTrue(config.isReminderEnabled)
        assertEquals(21, config.reminderHour)
        assertEquals(30, config.reminderMinute)
        assertEquals("1.0.1", config.appVersion)
        assertEquals(2, config.dataVersion)
        assertEquals(15, config.totalUsageCount)
    }

    /**
     * 测试用户配置的默认值
     */
    @Test
    fun testUserConfigDefaults() {
        val config = UserConfig()

        assertEquals(1, config.id)
        assertNull(config.nickname)
        assertNull(config.avatarPath)
        assertFalse(config.isDarkMode)
        assertTrue(config.isSoundEnabled)
        assertTrue(config.isVibrationEnabled)
        assertFalse(config.isReminderEnabled)
        assertEquals(20, config.reminderHour)
        assertEquals(0, config.reminderMinute)
        assertEquals("1.0.0", config.appVersion)
        assertEquals(1, config.dataVersion)
        assertTrue(config.firstUseTime > 0)
        assertEquals(0, config.totalUsageCount)
        assertTrue(config.lastUsedTime > 0)
    }

    /**
     * 测试提醒时间格式化
     */
    @Test
    fun testGetReminderTimeString() {
        val config1 = UserConfig(reminderHour = 9, reminderMinute = 5)
        assertEquals("09:05", config1.getReminderTimeString())

        val config2 = UserConfig(reminderHour = 23, reminderMinute = 59)
        assertEquals("23:59", config2.getReminderTimeString())

        val config3 = UserConfig(reminderHour = 0, reminderMinute = 0)
        assertEquals("00:00", config3.getReminderTimeString())
    }

    /**
     * 测试有效提醒时间检查
     */
    @Test
    fun testHasValidReminderTime() {
        // 有效的提醒时间
        val validConfig1 = UserConfig(isReminderEnabled = true, reminderHour = 10, reminderMinute = 30)
        assertTrue(validConfig1.hasValidReminderTime())

        val validConfig2 = UserConfig(isReminderEnabled = true, reminderHour = 0, reminderMinute = 0)
        assertTrue(validConfig2.hasValidReminderTime())

        val validConfig3 = UserConfig(isReminderEnabled = true, reminderHour = 23, reminderMinute = 59)
        assertTrue(validConfig3.hasValidReminderTime())

        // 无效的提醒时间
        val disabledConfig = UserConfig(isReminderEnabled = false, reminderHour = 10, reminderMinute = 30)
        assertFalse(disabledConfig.hasValidReminderTime())

        val invalidHourConfig1 = UserConfig(isReminderEnabled = true, reminderHour = -1, reminderMinute = 30)
        assertFalse(invalidHourConfig1.hasValidReminderTime())

        val invalidHourConfig2 = UserConfig(isReminderEnabled = true, reminderHour = 24, reminderMinute = 30)
        assertFalse(invalidHourConfig2.hasValidReminderTime())

        val invalidMinuteConfig1 = UserConfig(isReminderEnabled = true, reminderHour = 10, reminderMinute = -1)
        assertFalse(invalidMinuteConfig1.hasValidReminderTime())

        val invalidMinuteConfig2 = UserConfig(isReminderEnabled = true, reminderHour = 10, reminderMinute = 60)
        assertFalse(invalidMinuteConfig2.hasValidReminderTime())
    }

    /**
     * 测试昵称和头像检查方法
     */
    @Test
    fun testNicknameAndAvatarChecks() {
        // 有昵称和头像
        val fullConfig = UserConfig(nickname = "用户名", avatarPath = "/path/to/avatar.jpg")
        assertTrue(fullConfig.hasNickname())
        assertTrue(fullConfig.hasAvatar())

        // 只有昵称
        val nicknameOnlyConfig = UserConfig(nickname = "用户名")
        assertTrue(nicknameOnlyConfig.hasNickname())
        assertFalse(nicknameOnlyConfig.hasAvatar())

        // 只有头像
        val avatarOnlyConfig = UserConfig(avatarPath = "/path/to/avatar.jpg")
        assertFalse(avatarOnlyConfig.hasNickname())
        assertTrue(avatarOnlyConfig.hasAvatar())

        // 都没有
        val emptyConfig = UserConfig()
        assertFalse(emptyConfig.hasNickname())
        assertFalse(emptyConfig.hasAvatar())

        // 空字符串
        val emptyStringConfig = UserConfig(nickname = "", avatarPath = "")
        assertFalse(emptyStringConfig.hasNickname())
        assertFalse(emptyStringConfig.hasAvatar())
    }

    /**
     * 测试使用天数计算
     */
    @Test
    fun testGetUsageDays() {
        val currentTime = System.currentTimeMillis()
        val oneDayInMillis = 24 * 60 * 60 * 1000L

        // 当天使用
        val todayConfig = UserConfig(firstUseTime = currentTime)
        assertEquals(1, todayConfig.getUsageDays())

        // 1天前使用
        val oneDayAgoConfig = UserConfig(firstUseTime = currentTime - oneDayInMillis)
        assertEquals(2, oneDayAgoConfig.getUsageDays())

        // 10天前使用
        val tenDaysAgoConfig = UserConfig(firstUseTime = currentTime - 10 * oneDayInMillis)
        assertEquals(11, tenDaysAgoConfig.getUsageDays())
    }

    /**
     * 测试默认配置获取方法
     */
    @Test
    fun testGetDefault() {
        val defaultConfig = UserConfig.getDefault()

        assertEquals(1, defaultConfig.id)
        assertNull(defaultConfig.nickname)
        assertNull(defaultConfig.avatarPath)
        assertFalse(defaultConfig.isDarkMode)
        assertTrue(defaultConfig.isSoundEnabled)
        assertTrue(defaultConfig.isVibrationEnabled)
        assertFalse(defaultConfig.isReminderEnabled)
        assertEquals(20, defaultConfig.reminderHour)
        assertEquals(0, defaultConfig.reminderMinute)
        assertEquals("1.0.0", defaultConfig.appVersion)
        assertEquals(1, defaultConfig.dataVersion)
        assertTrue(defaultConfig.firstUseTime > 0)
        assertEquals(0, defaultConfig.totalUsageCount)
        assertTrue(defaultConfig.lastUsedTime > 0)
    }

    /**
     * 测试测试配置创建方法
     */
    @Test
    fun testCreateTestConfig() {
        val testConfig = UserConfig.createTestConfig("测试用户")

        assertEquals("测试用户", testConfig.nickname)
        assertFalse(testConfig.isDarkMode)
        assertTrue(testConfig.isSoundEnabled)
        assertTrue(testConfig.isVibrationEnabled)
        assertTrue(testConfig.isReminderEnabled)
        assertEquals(20, testConfig.reminderHour)
        assertEquals(0, testConfig.reminderMinute)
        assertEquals(10, testConfig.totalUsageCount)
        assertTrue(testConfig.lastUsedTime > 0)

        // 测试默认昵称
        val defaultTestConfig = UserConfig.createTestConfig()
        assertEquals("测试用户", defaultTestConfig.nickname)
    }

    /**
     * 测试配置ID的固定性
     */
    @Test
    fun testConfigIdFixed() {
        // 即使不指定ID，也应该默认为1
        val config1 = UserConfig()
        assertEquals(1, config1.id)

        val config2 = UserConfig(nickname = "用户")
        assertEquals(1, config2.id)

        // 指定ID时应该使用指定的值
        val config3 = UserConfig(id = 5)
        assertEquals(5, config3.id)
    }
}