import com.correx.core.approvals.GrantScope
import com.correx.core.approvals.Tier
import com.correx.core.approvals.domain.DefaultApprovalEngine
import com.correx.core.approvals.model.ApprovalContext
import com.correx.core.approvals.model.ApprovalGrant
import com.correx.core.approvals.model.ApprovalScopeIdentity
import com.correx.core.events.types.GrantId
import com.correx.core.events.types.SessionId
import com.correx.core.sessions.ApprovalMode
import com.correx.testing.fixtures.request
import kotlinx.datetime.Instant
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test

class TierImmutabilityTest {

    private val engine = DefaultApprovalEngine()

    @Test
    fun `engine never modifies tier - always returns original`() {
        val tiers = listOf(Tier.T0, Tier.T1, Tier.T2, Tier.T3, Tier.T4)
        val ctx = ApprovalContext(
            ApprovalScopeIdentity(SessionId("s1"), null, null),
            ApprovalMode.YOLO
        )
        val now = Instant.parse("2026-01-01T00:00:01Z")

        tiers.forEach { tier ->
            val req = request("r1", tier)
            val decision = engine.evaluate(req, ctx, emptyList(), now)
            Assertions.assertEquals(tier, decision.tier, "Tier must remain unchanged")
        }
    }

    @Test
    fun `grant does not change tier in decision`() {
        val tier = Tier.T3
        val req = request("r1", tier)
        val grant = ApprovalGrant(
            id = GrantId("g1"),
            scope = GrantScope.SESSION(),
            permittedTiers = setOf(tier),
            reason = "test",
            timestamp = Instant.parse("2026-01-01T00:00:00Z")
        )
        val ctx = ApprovalContext(
            ApprovalScopeIdentity(SessionId("s1"), null, null),
            ApprovalMode.DENY
        )
        val now = Instant.parse("2026-01-01T00:00:01Z")

        val decision = engine.evaluate(req, ctx, listOf(grant), now)
        Assertions.assertEquals(tier, decision.tier)
    }
}
