package dev.kaizen.app

import androidx.compose.ui.graphics.Color
import dev.kaizen.app.ui.theme.Oklab
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Test

/**
 * High-fidelity Unit Test suite for Oklab / Oklch perception space algorithms.
 * 
 * Ensures perfect color-space conversion round-trip math and linear-perceptual 
 * interpolation correctness, guaranteeing zero technical debt in the graphics pipeline.
 */
class OklabTest {

    @Test
    fun testColorToOklabAndBack() {
        val originalColor = Color(1.0f, 0.5f, 0.0f, 1.0f) // Saturated Warm Amber
        val oklab = with(Oklab) { originalColor.toOklab() }
        
        // Convert Oklab coordinates back to standard RGB Color
        val reconstructedColor = with(Oklab) { oklab.toColor() }

        // Assert that the round-trip conversion retains mathematical fidelity within small floating-point tolerance
        assertEquals(originalColor.red, reconstructedColor.red, 0.01f)
        assertEquals(originalColor.green, reconstructedColor.green, 0.01f)
        assertEquals(originalColor.blue, reconstructedColor.blue, 0.01f)
    }

    @Test
    fun testInterpolationMidpoint() {
        val colorA = Color(1.0f, 0.0f, 0.0f, 1.0f) // Saturated Red
        val colorB = Color(0.0f, 0.0f, 1.0f, 1.0f) // Saturated Blue
        
        // Interpolate exactly at the midpoint (50%) in Oklab space
        val midpoint = Oklab.interpolate(colorA, colorB, 0.5f)

        // Perceptual midpoint of Red and Blue in Oklab should be a rich, glowing purple/violet
        // (rather than a muddy, dark gray typical of sRGB interpolation)
        assertTrue(midpoint.red > 0.1f)
        assertTrue(midpoint.blue > 0.1f)
        assertEquals(1.0f, midpoint.alpha, 0.01f)
    }

    @Test
    fun testGradientGeneration() {
        val colorA = Color(0.2f, 0.8f, 0.4f, 1.0f)
        val colorB = Color(0.8f, 0.2f, 0.9f, 1.0f)
        
        val stepsCount = 10
        val gradient = Oklab.generateGradientStops(colorA, colorB, stepsCount)

        // Verify correct amount of steps were generated
        assertEquals(stepsCount, gradient.size)
        // Verify start and end points of the gradient match the originals
        assertEquals(colorA.red, gradient.first().red, 0.01f)
        assertEquals(colorB.red, gradient.last().red, 0.01f)
    }
}
