import com.correx.core.approvals.ApprovalOutcome
import com.correx.core.approvals.ApprovalStatus
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.ApprovalScopeIdentity
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.assertEquals
import org.junit.jupiter.api.Test

class ModeApprovalTest {

    private val engine = DefaultApprovalEngine()

    private fun contextForMode(mode: ApprovalMode) = ApprovalContext(
        ApprovalScopeIdentity(SessionId("s1"), null, null),
        mode
    )

    private val now = Instant.parse("2026-01-01T00:00:01Z")

    @Test
    fun `DENY mode allows only T0`() {
        val ctx = contextForMode(ApprovalMode.DENY)
        assertEquals(
            ApprovalOutcome.AUTO_APPROVED,
            engine.evaluate(request("r1", Tier.T0), ctx, emptyList(), now).outcome
        )
        assertEquals(
            ApprovalStatus.PENDING,
            engine.evaluate(request("r2", Tier.T1), ctx, emptyList(), now).state
        )
        assertEquals(
            ApprovalStatus.PENDING,
            engine.evaluate(request("r3", Tier.T4), ctx, emptyList(), now).state
        )
    }

    @Test
    fun `PROMPT mode allows T0-T1`() {
        val ctx = contextForMode(ApprovalMode.PROMPT)
        assertEquals(
            ApprovalOutcome.AUTO_APPROVED,
            engine.evaluate(request("r1", Tier.T0), ctx, emptyList(), now).outcome
        )
        assertEquals(
            ApprovalOutcome.AUTO_APPROVED,
            engine.evaluate(request("r2", Tier.T1), ctx, emptyList(), now).outcome
        )
        assertEquals(
            ApprovalStatus.PENDING,
            engine.evaluate(request("r3", Tier.T2), ctx, emptyList(), now).state
        )
    }

    @Test
    fun `AUTO mode allows T0-T2`() {
        val ctx = contextForMode(ApprovalMode.AUTO)
        assertEquals(
            ApprovalOutcome.AUTO_APPROVED,
            engine.evaluate(request("r1", Tier.T0), ctx, emptyList(), now).outcome
        )
        assertEquals(
            ApprovalOutcome.AUTO_APPROVED,
            engine.evaluate(request("r2", Tier.T2), ctx, emptyList(), now).outcome
        )
        assertEquals(
            ApprovalStatus.PENDING,
            engine.evaluate(request("r3", Tier.T3), ctx, emptyList(), now).state
        )
    }

    @Test
    fun `YOLO mode allows all tiers, including T4`() {
        val ctx = contextForMode(ApprovalMode.YOLO)
        assertEquals(
            ApprovalOutcome.AUTO_APPROVED,
            engine.evaluate(request("r1", Tier.T0), ctx, emptyList(), now).outcome
        )
        assertEquals(
            ApprovalOutcome.AUTO_APPROVED,
            engine.evaluate(request("r2", Tier.T4), ctx, emptyList(), now).outcome
        )
    }
}
