#!/usr/bin/env bash
# Verify accessibility identifier parity between iOS and Android test harnesses.
#
# Extracts every string literal from both the Swift and Kotlin ID catalogs
# and fails if any iOS-defined ID is missing from the Kotlin side. Kotlin is
# allowed to be a superset (Android-only test hooks are fine); iOS ⊆ Kotlin
# is the invariant the test suites rely on.
set -euo pipefail

cd "$(dirname "$0")/.."

ios_file="iosApp/HoneyDueUITests/AccessibilityIdentifiers.swift"
kotlin_file="composeApp/src/commonMain/kotlin/com/tt/honeyDue/testing/AccessibilityIds.kt"

if [ ! -f "$ios_file" ]; then
    echo "ERROR: iOS catalog not found at $ios_file" >&2
    exit 1
fi
if [ ! -f "$kotlin_file" ]; then
    echo "ERROR: Kotlin catalog not found at $kotlin_file" >&2
    exit 1
fi

ios_ids=$(mktemp)
kotlin_ids=$(mktemp)
trap 'rm -f "$ios_ids" "$kotlin_ids"' EXIT

# Extract quoted string literals but ignore any string containing interpolation
# tokens (Swift `\(...)` or Kotlin `$` / `${...}`). Those are illustrative doc
# examples, not real accessibility IDs to compare.
grep -oE '"[^"]+"' "$ios_file" | grep -v '\\(' | sort -u > "$ios_ids"
grep -oE '"[^"]+"' "$kotlin_file" | grep -v '\$' | sort -u > "$kotlin_ids"

echo "iOS-only IDs (missing in Android):"
comm -23 "$ios_ids" "$kotlin_ids"
echo ""
echo "Android-only IDs (not in iOS):"
comm -13 "$ios_ids" "$kotlin_ids"

missing=$(comm -23 "$ios_ids" "$kotlin_ids" | wc -l | tr -d ' ')
if [ "$missing" != "0" ]; then
    echo ""
    echo "FAIL: $missing iOS ID(s) have no Kotlin counterpart." >&2
    exit 1
fi

echo ""
echo "OK: all iOS accessibility IDs have Kotlin counterparts."
