package com.correx.core.tools.state

import com.correx.core.approvals.Tier
import com.correx.core.events.events.ToolRequest
import com.correx.core.events.types.SessionId
import com.correx.core.events.types.StageId
import com.correx.core.events.types.ToolInvocationId
import kotlinx.datetime.Instant
import org.junit.jupiter.api.Test
import kotlin.test.assertEquals
import kotlin.test.assertNull
import kotlin.test.assertTrue

class ToolStateTest {

    private val request = ToolRequest(
        invocationId = ToolInvocationId("inv-1"),
        sessionId = SessionId("s-1"),
        stageId = StageId("st-1"),
        toolName = "echo"
    )

    @Test
    fun `initial ToolState has empty invocations`() {
        assertTrue(ToolState().invocations.isEmpty())
    }

    @Test
    fun `ToolInvocationRecord has null receipt and completedAt by default`() {
        val record = ToolInvocationRecord(
            invocationId = ToolInvocationId("inv-1"),
            toolName = "echo",
            tier = Tier.T0,
            request = request,
            status = ToolInvocationStatus.REQUESTED,
            requestedAt = Instant.parse("2026-01-01T00:00:00Z")
        )
        assertNull(record.receipt)
        assertNull(record.completedAt)
    }

    @Test
    fun `ToolState can accumulate multiple records`() {
        val base = ToolInvocationRecord(
            invocationId = ToolInvocationId("inv-1"),
            toolName = "echo",
            tier = Tier.T0,
            request = request,
            status = ToolInvocationStatus.REQUESTED,
            requestedAt = Instant.parse("2026-01-01T00:00:00Z")
        )
        val state = ToolState(
            invocations = listOf(base, base.copy(invocationId = ToolInvocationId("inv-2")))
        )
        assertEquals(2, state.invocations.size)
    }
}
