package com.tt.honeyDue.screenshot

import com.tt.honeyDue.testing.GalleryScreens
import com.tt.honeyDue.testing.Platform
import org.junit.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue

/**
 * Parity gate — asserts [gallerySurfaces] is exactly the set of screens
 * declared in [GalleryScreens] with [Platform.ANDROID] in their platforms.
 *
 * If this fails, either:
 *   - A new screen was added to [gallerySurfaces] but missing from the
 *     canonical manifest — update [GalleryScreens.all].
 *   - A new screen was added to the manifest but not wired into
 *     [gallerySurfaces] — add the corresponding `GallerySurface(...)`
 *     entry.
 *   - A rename landed on only one side — reconcile.
 *
 * This keeps Android and iOS from silently drifting apart in coverage.
 * The iOS equivalent (`GalleryManifestParityTest.swift`) enforces the
 * same invariant on the Swift test file.
 */
class GalleryManifestParityTest {

    @Test
    fun android_surfaces_match_manifest_exactly() {
        val actual = gallerySurfaces.map { it.name }.toSet()
        val expected = GalleryScreens.forAndroid.keys

        val missing = expected - actual
        val extra = actual - expected

        if (missing.isNotEmpty() || extra.isNotEmpty()) {
            val message = buildString {
                appendLine("Android GallerySurfaces drifted from canonical manifest.")
                if (missing.isNotEmpty()) {
                    appendLine()
                    appendLine("Screens in manifest but missing from GallerySurfaces.kt:")
                    missing.sorted().forEach { appendLine("  - $it") }
                }
                if (extra.isNotEmpty()) {
                    appendLine()
                    appendLine("Screens in GallerySurfaces.kt but missing from manifest:")
                    extra.sorted().forEach { appendLine("  - $it") }
                }
                appendLine()
                appendLine("Reconcile by editing com.tt.honeyDue.testing.GalleryScreens and/or")
                appendLine("com.tt.honeyDue.screenshot.GallerySurfaces so both agree.")
            }
            kotlin.test.fail(message)
        }
        assertEquals(expected, actual)
    }

    @Test
    fun no_duplicate_surface_names() {
        val duplicates = gallerySurfaces.map { it.name }
            .groupingBy { it }
            .eachCount()
            .filterValues { it > 1 }
        assertTrue(
            duplicates.isEmpty(),
            "Duplicate surface names in GallerySurfaces.kt: $duplicates",
        )
    }
}
