#!/usr/bin/env bash
# Test suite for layout-spec expand library
set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
EXPAND="$SCRIPT_DIR/../expand.sh"

PASS=0
FAIL=0

assert_eq() {
    local desc="$1" expected="$2"
    local actual
    actual=$("$EXPAND" "$3" 2>/dev/null) || true
    if [ "$expected" = "$actual" ]; then
        echo "  PASS: $desc"
        PASS=$((PASS + 1))
    else
        echo "  FAIL: $desc"
        echo "    expected: $expected"
        echo "    actual:   $actual"
        FAIL=$((FAIL + 1))
    fi
}

assert_error() {
    local desc="$1" input="$2"
    if ! "$EXPAND" "$input" >/dev/null 2>&1; then
        echo "  PASS: $desc"
        PASS=$((PASS + 1))
    else
        echo "  FAIL: $desc (expected error)"
        FAIL=$((FAIL + 1))
    fi
}

assert_warning() {
    local desc="$1" expected_out="$2" input="$3"
    local actual stderr_out
    stderr_out=$("$EXPAND" "$input" 2>&1 1>/dev/null) || true
    actual=$("$EXPAND" "$input" 2>/dev/null) || true
    if [ "$expected_out" = "$actual" ] && echo "$stderr_out" | grep -q "WARNING"; then
        echo "  PASS: $desc"
        PASS=$((PASS + 1))
    else
        echo "  FAIL: $desc"
        echo "    expected output: $expected_out  (got: $actual)"
        echo "    expected WARNING in stderr (got: $stderr_out)"
        FAIL=$((FAIL + 1))
    fi
}

echo "=== layout-spec test suite ==="
echo

echo "— Basic expansion —"
assert_eq "single font size" "text-18" "fs-18"
assert_eq "single font weight" "font-medium" "fw-medium"
assert_eq "single spacing" "mb-20" "mb-20"
assert_eq "combined typography + spacing" "text-18 font-medium mb-20" "fs-18,fw-medium,mb-20"
assert_eq "bold + large" "text-40 font-bold mb-30" "fs-40,fw-bold,mb-30"

echo
echo "— Color passthrough —"
assert_eq "text color" "text-orange" "text-orange"
assert_eq "bg color" "bg-pink" "bg-pink"
assert_eq "border color" "border-gray-300" "border-gray-300"
assert_eq "compound color" "bg-gray-100" "bg-gray-100"

echo
echo "— Responsive variants —"
assert_eq "responsive spacing mb-lg-80" "lg:mb-80" "mb-lg-80"
assert_eq "responsive font size fs-md-22" "md:text-22" "fs-md-22"
assert_eq "responsive color bg-lg-gray-100" "lg:bg-gray-100" "bg-lg-gray-100"
assert_eq "responsive padding px-md-30" "md:px-30" "px-md-30"
assert_eq "responsive text color text-sm-orange" "sm:text-orange" "text-sm-orange"
assert_eq "mixed responsive" "mb-40 lg:mb-80 md:pt-10" "mb-40,mb-lg-80,pt-md-10"

echo
echo "— Combined (typography + spacing + responsive) —"
assert_eq "full combo" "text-18 md:text-22 font-medium leading-normal mb-20 lg:mb-40" \
    "fs-18,fs-md-22,fw-medium,lh-1.4,mb-20,mb-lg-40"

echo
echo "— Named spacing tokens —"
assert_eq "gap-gutter" "gap-gutter" "gap-gutter"

echo
echo "— Two-segment prefixes —"
assert_eq "gap-x passthrough" "gap-x-20" "gap-x-20"
assert_eq "gap-x responsive" "md:gap-x-20" "gap-x-md-20"

echo
echo "— Line height —"
assert_eq "mapped line height" "leading-normal" "lh-1.4"
assert_eq "mapped leading-none" "leading-none" "lh-1"
assert_warning "unmapped line height fallback" "leading-[1.8]" "lh-1.8"

echo
echo "— Inline ![...] expansion —"
assert_eq "inline simple" "foo text-18 font-medium bar" 'foo ![fs-18,fw-medium] bar'
assert_eq "inline in class attr" 'class="text-40 font-bold mb-30 text-balance"' \
    'class="![fs-40,fw-bold,mb-30] text-balance"'
assert_eq "inline multiple markers" "a text-18 b font-bold c" 'a ![fs-18] b ![fw-bold] c'

echo
echo "— Hard errors —"
assert_error "invalid font size fs-99" "fs-99"
assert_error "unknown prefix xx-10" "xx-10"
assert_error "bare prefix with no value" "fs"

echo
echo "=== Results: $PASS passed, $FAIL failed ==="
[ "$FAIL" -eq 0 ] && exit 0 || exit 1
