package com.correx.apps.cli.commands

import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Test

class StatsRenderTest {

    private val report = StatsReportDto(
        sessionId = "abc-123",
        eventCount = 12,
        sessionDurationMs = 90_000,
        inferenceCount = 3,
        inferenceMs = 6000,
        promptTokens = 310,
        completionTokens = 170,
        tokensPerSecond = 28.3,
        perProvider = listOf(
            ProviderStatsDto("lfm:a", 2, 5000, 300, 150, 30.0),
            ProviderStatsDto("lfm:b", 1, 1000, 10, 20, 20.0),
        ),
        toolCount = 3,
        toolMs = 1500,
        perTool = listOf(ToolStatsDto("shell.read", 2, 1200)),
        approvalsRequested = 2,
        approvalsResolved = 1,
        approvalsPending = 1,
        approvalWaitMs = 30_000,
        avgApprovalWaitMs = 30_000,
        perTier = listOf(TierApprovalStatsDto("T2", 2, 1, 30_000, 30_000)),
        failures = FailureMetricsDto(inferenceFailures = 1, toolFailures = 1, workflowFailures = 1),
        inferencePct = 6.7,
        toolPct = 1.7,
        approvalWaitPct = 33.3,
    )

    @Test
    fun `render includes session id and all section headers`() {
        val out = renderStats(report)
        assertTrue(out.contains("Session:    abc-123"))
        assertTrue(out.contains("Inference"))
        assertTrue(out.contains("Tools"))
        assertTrue(out.contains("Approvals"))
        assertTrue(out.contains("Failures"))
        assertTrue(out.contains("Time accounting"))
    }

    @Test
    fun `render shows per-provider and per-tier breakdown rows`() {
        val out = renderStats(report)
        assertTrue(out.contains("lfm:a"), "provider row missing")
        assertTrue(out.contains("lfm:b"), "provider row missing")
        assertTrue(out.contains("shell.read"), "tool row missing")
        assertTrue(out.contains("T2"), "tier row missing")
    }

    @Test
    fun `render formats duration as human-readable`() {
        // 90_000 ms -> 1m 30s
        assertTrue(renderStats(report).contains("1m 30s"), "duration not humanized")
    }

    @Test
    fun `time accounting other is the remainder of the three tracked categories`() {
        // 100 - 6.7 - 1.7 - 33.3 = 58.3
        assertTrue(renderStats(report).contains("other: 58.3%"), "remainder miscomputed")
    }
}
