#!/bin/bash
# test_endpoints.sh
# Validates all ABCC server endpoints without needing a Roku device.
#
# Usage: bash scripts/test_endpoints.sh

BASE="https://media.abcc.org"
FAILURES=0

pass() { echo "  [PASS] $1"; }
fail() { echo "  [FAIL] $1"; FAILURES=$((FAILURES + 1)); }

check_url() {
    local label="$1" url="$2" expect="$3"
    local http_code
    http_code=$(curl -s -o /dev/null -w "%{http_code}" "$url")
    if [ "$http_code" = "$expect" ]; then
        pass "$label → HTTP $http_code"
    else
        fail "$label → expected HTTP $expect, got $http_code ($url)"
    fi
}

check_json_field() {
    local label="$1" url="$2" field="$3"
    local val
    val=$(curl -s "$url" | jq -r "$field" 2>/dev/null)
    if [ -n "$val" ] && [ "$val" != "null" ]; then
        pass "$label ($field = $val)"
    else
        fail "$label — field '$field' missing or null in $url"
    fi
}

check_header() {
    local label="$1" url="$2" header="$3" expect="$4"
    local val
    val=$(curl -sI "$url" | grep -i "^$header:" | tr -d '\r' | cut -d' ' -f2-)
    if echo "$val" | grep -qi "$expect"; then
        pass "$label ($header: $val)"
    else
        fail "$label — expected '$expect' in $header, got '$val'"
    fi
}

echo "=== ABCC Endpoint Tests ==="
echo ""

# -----------------------------------------------------------------------
echo "[1/4] Primary content feed"
# -----------------------------------------------------------------------
check_url       "Feed reachable"        "$BASE/roku/feeds/primary-feed.json" "200"
check_json_field "Feed has providerName" "$BASE/roku/feeds/primary-feed.json" ".providerName"
check_json_field "Feed has content rows" "$BASE/roku/feeds/primary-feed.json" '[to_entries[] | select(.value | type=="array")] | .[0].value[0].title'

# -----------------------------------------------------------------------
echo ""
echo "[2/4] Now Playing endpoint"
# -----------------------------------------------------------------------
check_url        "now-playing reachable"  "$BASE/roku/now-playing.json" "200"
check_json_field "now-playing has title"  "$BASE/roku/now-playing.json" ".title"
check_json_field "now-playing has block"  "$BASE/roku/now-playing.json" ".blockStart"
check_json_field "now-playing has time"   "$BASE/roku/now-playing.json" ".updatedAt"

# -----------------------------------------------------------------------
echo ""
echo "[3/4] Live stream"
# -----------------------------------------------------------------------
LIVE_URL="$BASE/live/stream.m3u8"
LIVE_STATUS=$(curl -s -o /dev/null -w "%{http_code}" "$LIVE_URL")

if [ "$LIVE_STATUS" = "200" ]; then
    pass "Live stream playlist reachable → HTTP 200"
    check_header "Live MIME type"  "$LIVE_URL" "content-type" "mpegurl"
    check_header "Live no-cache"   "$LIVE_URL" "cache-control" "no-cache"
    check_header "Live CORS"       "$LIVE_URL" "access-control-allow-origin" "*"

    # Probe with ffprobe
    if ffprobe -v quiet -show_streams "$LIVE_URL" 2>/dev/null | grep -q "codec_type"; then
        pass "Live stream is playable (ffprobe detected streams)"
    else
        fail "Live stream not playable — is abcc-live service running?"
    fi
else
    echo "  [SKIP] Live stream not up (HTTP $LIVE_STATUS) — start abcc-live to test"
fi

# -----------------------------------------------------------------------
echo ""
echo "[4/4] Browser test page"
# -----------------------------------------------------------------------
check_url "Test UI reachable" "$BASE/roku/test.html" "200"

# -----------------------------------------------------------------------
echo ""
echo "=== Results: $FAILURES failure(s) ==="
[ "$FAILURES" -eq 0 ] && echo "All endpoints OK." || echo "Fix failures above."
echo ""
echo "Browser test UI: $BASE/roku/test.html"
exit $FAILURES
