package com.chick_mood.data.model

import kotlin.random.Random

/**
 * 测试数据生成器
 * 为单元测试提供各种测试数据
 *
 * @author Claude
 * @date 2025-10-22
 */
object TestDataGenerator {

    private val random = Random(System.currentTimeMillis())

    /**
     * 生成随机的心情记录
     * @param count 生成的记录数量
     * @return 心情记录列表
     */
    fun generateMoodRecords(count: Int): List<MoodRecord> {
        return (1..count).map { index ->
            generateMoodRecord(index.toLong())
        }
    }

    /**
     * 生成单个心情记录
     * @param id 记录ID
     * @param timestamp 时间戳（可选，默认为随机生成）
     * @return 心情记录
     */
    fun generateMoodRecord(
        id: Long = 0,
        timestamp: Long = generateRandomTimestamp()
    ): MoodRecord {
        val emotions = Emotion.values()
        val randomEmotion = emotions[random.nextInt(emotions.size)]
        val randomIntensity = random.nextInt(MoodRecord.MIN_INTENSITY, MoodRecord.MAX_INTENSITY + 1)
        val randomText = generateRandomText()
        val randomImage = if (random.nextBoolean()) generateRandomImagePath() else null
        val randomFavorite = random.nextBoolean()
        val randomShakeDuration = random.nextFloat() * 10f + 1f // 1-11秒
        val randomMaxAcceleration = random.nextFloat() * 30f + 5f // 5-35

        return MoodRecord(
            id = id,
            emotion = randomEmotion,
            moodIntensity = randomIntensity,
            timestamp = timestamp,
            textContent = randomText,
            imagePath = randomImage,
            isFavorite = randomFavorite,
            shakeDuration = randomShakeDuration,
            maxAcceleration = randomMaxAcceleration,
            deviceModel = generateRandomDeviceModel()
        )
    }

    /**
     * 生成特定情绪的心情记录
     * @param emotion 指定的情绪类型
     * @param count 生成的记录数量
     * @return 指定情绪的心情记录列表
     */
    fun generateMoodRecordsByEmotion(emotion: Emotion, count: Int): List<MoodRecord> {
        return (1..count).map { index ->
            MoodRecord(
                id = index.toLong(),
                emotion = emotion,
                moodIntensity = random.nextInt(MoodRecord.MIN_INTENSITY, MoodRecord.MAX_INTENSITY + 1),
                timestamp = generateRandomTimestamp(),
                textContent = generateRandomText(),
                imagePath = if (random.nextBoolean()) generateRandomImagePath() else null,
                isFavorite = random.nextBoolean(),
                shakeDuration = random.nextFloat() * 10f + 1f,
                maxAcceleration = random.nextFloat() * 30f + 5f,
                deviceModel = generateRandomDeviceModel()
            )
        }
    }

    /**
     * 生成收藏的心情记录
     * @param count 生成的记录数量
     * @return 收藏的心情记录列表
     */
    fun generateFavoriteMoodRecords(count: Int): List<MoodRecord> {
        return (1..count).map { index ->
            MoodRecord(
                id = index.toLong(),
                emotion = Emotion.values().random(),
                moodIntensity = random.nextInt(MoodRecord.MIN_INTENSITY, MoodRecord.MAX_INTENSITY + 1),
                timestamp = generateRandomTimestamp(),
                textContent = generateRandomText(),
                imagePath = if (random.nextBoolean()) generateRandomImagePath() else null,
                isFavorite = true, // 强制设置为收藏
                shakeDuration = random.nextFloat() * 10f + 1f,
                maxAcceleration = random.nextFloat() * 30f + 5f,
                deviceModel = generateRandomDeviceModel()
            )
        }
    }

    /**
     * 生成今天的心情记录
     * @param count 生成的记录数量
     * @return 今天的心情记录列表
     */
    fun generateTodayMoodRecords(count: Int): List<MoodRecord> {
        val todayStart = System.currentTimeMillis() / (24 * 60 * 60 * 1000) * (24 * 60 * 60 * 1000)
        return (1..count).map { index ->
            val timestamp = todayStart + random.nextInt(24 * 60 * 60 * 1000)
            generateMoodRecord(index.toLong(), timestamp)
        }
    }

    /**
     * 生成测试用户配置
     * @param nickname 用户昵称
     * @return 用户配置
     */
    fun generateUserConfig(nickname: String = "测试用户"): UserConfig {
        return UserConfig(
            id = 1,
            nickname = nickname,
            avatarPath = if (random.nextBoolean()) generateRandomAvatarPath() else null,
            isDarkMode = random.nextBoolean(),
            isSoundEnabled = random.nextBoolean(),
            isVibrationEnabled = random.nextBoolean(),
            isReminderEnabled = random.nextBoolean(),
            reminderHour = random.nextInt(24),
            reminderMinute = random.nextInt(60),
            appVersion = "1.0.0",
            dataVersion = 1,
            firstUseTime = System.currentTimeMillis() - random.nextLong(0, 30L * 24 * 60 * 60 * 1000), // 30天内
            totalUsageCount = random.nextInt(100),
            lastUsedTime = System.currentTimeMillis()
        )
    }

    /**
     * 生成随机时间戳（最近30天内）
     * @return 随机时间戳
     */
    private fun generateRandomTimestamp(): Long {
        val thirtyDaysInMillis = 30L * 24 * 60 * 60 * 1000
        val currentTime = System.currentTimeMillis()
        return currentTime - random.nextLong(0, thirtyDaysInMillis)
    }

    /**
     * 生成随机文字内容
     * @return 随机文字内容
     */
    private fun generateRandomText(): String {
        val texts = listOf(
            "今天心情不错",
            "工作有点累",
            "和朋友出去玩很开心",
            "学习新知识很有成就感",
            "天气真好",
            "有点想念家人",
            "今天遇到了有趣的事情",
            "工作顺利完成",
            "收到了好消息",
            "期待周末的到来",
            "今天运动了，感觉很棒",
            "读到一本好书",
            "和朋友的聊天很开心",
            "今天的 sunset 很美",
            "尝试了新的餐厅",
            "完成了重要的项目",
            "今天很有灵感",
            "收到了朋友的关心",
            "今天的心情像天气一样晴朗",
            "生活中的小确幸",
            "" // 空文字
        )
        return texts.random()
    }

    /**
     * 生成随机图片路径
     * @return 随机图片路径
     */
    private fun generateRandomImagePath(): String {
        val imageNames = listOf(
            "mood_image_001.jpg",
            "mood_image_002.png",
            "mood_image_003.jpg",
            "screenshot_001.png",
            "photo_20231022.jpg",
            "camera_image.jpg"
        )
        return "/storage/emulated/0/Pictures/MoodApp/${imageNames.random()}"
    }

    /**
     * 生成随机头像路径
     * @return 随机头像路径
     */
    private fun generateRandomAvatarPath(): String {
        val avatarNames = listOf(
            "avatar_001.jpg",
            "avatar_002.png",
            "profile_pic.jpg",
            "user_avatar.png"
        )
        return "/storage/emulated/0/Pictures/MoodApp/avatars/${avatarNames.random()}"
    }

    /**
     * 生成随机设备型号
     * @return 随机设备型号
     */
    private fun generateRandomDeviceModel(): String {
        val deviceModels = listOf(
            "SM-G998B", // Samsung Galaxy S21 Ultra
            "Pixel 6 Pro", // Google Pixel 6 Pro
            "iPhone14,3", // iPhone 14 Pro
            "Mi 11", // Xiaomi Mi 11
            "OnePlus 9 Pro", // OnePlus 9 Pro
            "CPH2491", // OnePlus 9RT
            "LM-G900", // LG G8
            "HUAWEI P40", // Huawei P40
            "Galaxy S22", // Samsung Galaxy S22
            "Nokia 8.3" // Nokia 8.3
        )
        return deviceModels.random()
    }

    /**
     * 生成完整的测试数据集
     * @return 包含心情记录和用户配置的配对
     */
    fun generateFullTestData(): Pair<List<MoodRecord>, UserConfig> {
        val moodRecords = generateMoodRecords(20)
        val userConfig = generateUserConfig()
        return Pair(moodRecords, userConfig)
    }

    /**
     * 生成边界情况测试数据
     * @return 包含各种边界情况的心情记录列表
     */
    fun generateBoundaryTestRecords(): List<MoodRecord> {
        return listOf(
            // 最小强度
            MoodRecord.createTestRecord(Emotion.HAPPY, MoodRecord.MIN_INTENSITY, "最小强度测试"),
            // 最大强度
            MoodRecord.createTestRecord(Emotion.ANGRY, MoodRecord.MAX_INTENSITY, "最大强度测试"),
            // 最长文字
            MoodRecord.createTestRecord(
                Emotion.SAD,
                50,
                "这是一个很长的测试文字".repeat(20).take(MoodRecord.MAX_TEXT_LENGTH)
            ),
            // 空内容
            MoodRecord(
                emotion = Emotion.WORRIED,
                moodIntensity = 30,
                timestamp = System.currentTimeMillis(),
                textContent = null,
                imagePath = null
            ),
            // 收藏记录
            MoodRecord.createTestRecord(Emotion.LONELY, 60, "收藏记录测试").copy(isFavorite = true)
        )
    }
}