package dev.jasonpearson.automobile.desktop.core.performance

typealias HealthStatus = dev.jasonpearson.automobile.desktop.domain.HealthStatus
typealias MetricType = dev.jasonpearson.automobile.desktop.domain.MetricType
typealias PerformanceMetric = dev.jasonpearson.automobile.desktop.domain.PerformanceMetric
typealias MetricTrend = dev.jasonpearson.automobile.desktop.domain.MetricTrend
typealias MetricDataPoint = dev.jasonpearson.automobile.desktop.domain.MetricDataPoint
typealias PerformanceAnomaly = dev.jasonpearson.automobile.desktop.domain.PerformanceAnomaly
typealias PerformanceRun = dev.jasonpearson.automobile.desktop.domain.PerformanceRun
typealias RunComparison = dev.jasonpearson.automobile.desktop.domain.RunComparison
typealias MetricChange = dev.jasonpearson.automobile.desktop.domain.MetricChange
typealias PerformanceThresholds = dev.jasonpearson.automobile.desktop.domain.PerformanceThresholds

// Mock data for development
object PerformanceMockData {
    private const val BASE_TIME = 1705000000000L

    private fun generateHistory(
        baseValue: Float,
        variance: Float,
        count: Int,
        screens: List<String>,
    ): List<MetricDataPoint> {
        return (0 until count).map { i ->
            MetricDataPoint(
                timestamp = BASE_TIME + i * 1000L,
                value = baseValue + (Math.random().toFloat() - 0.5f) * variance * 2,
                screenName = screens.getOrNull(i % screens.size),
            )
        }
    }

    // Generate FPS time series (typically high with occasional dips)
    private fun generateFpsHistory(count: Int, screens: List<String>): List<MetricDataPoint> {
        return (0 until count).map { i ->
            val baseFps = 60f
            // Simulate occasional frame drops
            val dip = if (i % 12 == 0) (Math.random().toFloat() * 20f) else 0f
            MetricDataPoint(
                timestamp = BASE_TIME + i * 16L,  // ~60fps timing
                value = (baseFps - dip).coerceAtLeast(30f),
                screenName = screens.getOrNull(i % screens.size),
            )
        }
    }

    val screens = listOf("Splash", "Login", "Home", "ChatList", "Chat", "Profile", "Settings")

    val touchLatencyMetric = PerformanceMetric(
        id = "touch_latency",
        type = MetricType.TouchLatency,
        name = "Touch Latency",
        currentValue = 42f,
        unit = "ms",
        thresholdWarning = 100f,
        thresholdCritical = 200f,
        trend = MetricTrend.Stable,
        history = generateHistory(45f, 20f, 60, screens),
    )

    val timeToFirstFrameMetric = PerformanceMetric(
        id = "ttff",
        type = MetricType.TimeToFirstFrame,
        name = "Time to First Frame",
        currentValue = 320f,
        unit = "ms",
        thresholdWarning = 500f,
        thresholdCritical = 1000f,
        trend = MetricTrend.Down,  // Improving
        history = generateHistory(350f, 80f, 60, screens),
    )

    val timeToInteractiveMetric = PerformanceMetric(
        id = "tti",
        type = MetricType.TimeToInteractive,
        name = "Time to Interactive",
        currentValue = 580f,
        unit = "ms",
        thresholdWarning = 1000f,
        thresholdCritical = 2000f,
        trend = MetricTrend.Stable,
        history = generateHistory(600f, 150f, 60, screens),
    )

    val jankMetric = PerformanceMetric(
        id = "jank",
        type = MetricType.Jank,
        name = "Jank (Missed Frames)",
        currentValue = 3f,  // Number of missed frames during startup
        unit = "frames",
        thresholdWarning = 5f,
        thresholdCritical = 10f,
        trend = MetricTrend.Up,  // Getting worse
        history = generateHistory(4f, 3f, 60, screens),
    )

    val fpsMetric = PerformanceMetric(
        id = "fps",
        type = MetricType.FPS,
        name = "Frame Rate",
        currentValue = 58f,
        unit = "fps",
        thresholdWarning = 45f,
        thresholdCritical = 30f,
        trend = MetricTrend.Stable,
        history = generateFpsHistory(120, screens),  // Higher resolution for FPS
    )

    val allMetrics = listOf(
        touchLatencyMetric,
        timeToFirstFrameMetric,
        timeToInteractiveMetric,
        jankMetric,
        fpsMetric,
    )

    val anomalies = listOf(
        PerformanceAnomaly(
            id = "anom1",
            metricType = MetricType.Jank,
            severity = HealthStatus.Warning,
            message = "Frame drops during list scroll",
            timestamp = BASE_TIME + 15000,
            screenName = "ChatList",
            testName = "testSendMessage",
            value = 8f,
            threshold = 5f,
        ),
        PerformanceAnomaly(
            id = "anom2",
            metricType = MetricType.FPS,
            severity = HealthStatus.Warning,
            message = "FPS drop during scroll animation",
            timestamp = BASE_TIME + 28000,
            screenName = "Chat",
            testName = "testSendMessage",
            value = 38f,
            threshold = 45f,
        ),
        PerformanceAnomaly(
            id = "anom3",
            metricType = MetricType.TouchLatency,
            severity = HealthStatus.Critical,
            message = "High touch latency on button press",
            timestamp = BASE_TIME + 42000,
            screenName = "Login",
            testName = "testLoginFlow",
            value = 250f,
            threshold = 200f,
        ),
        PerformanceAnomaly(
            id = "anom4",
            metricType = MetricType.TimeToFirstFrame,
            severity = HealthStatus.Warning,
            message = "Slow first frame render",
            timestamp = BASE_TIME + 55000,
            screenName = "Home",
            testName = null,
            value = 650f,
            threshold = 500f,
        ),
    )

    val currentRun = PerformanceRun(
        id = "run_current",
        name = "Current Session",
        timestamp = BASE_TIME,
        durationMs = 60000,
        deviceName = "Pixel 8 API 35",
        overallHealth = HealthStatus.Warning,
        metrics = allMetrics,
        anomalies = anomalies,
        screensAnalyzed = screens,
    )

    val previousRun = PerformanceRun(
        id = "run_previous",
        name = "Previous Run",
        timestamp = BASE_TIME - 86400000,  // Yesterday
        durationMs = 55000,
        deviceName = "Pixel 8 API 35",
        overallHealth = HealthStatus.Healthy,
        metrics = allMetrics.map { it.copy(currentValue = it.currentValue * 0.9f) },
        anomalies = anomalies.take(1),
        screensAnalyzed = screens,
    )

    val recentRuns = listOf(currentRun, previousRun)
}
