#!/usr/bin/env bash
# wiki-lint-test.sh — Test-Suite für scripts/wiki-lint.sh
# Usage: ./scripts/wiki-lint-test.sh

set -u

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
REPO_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
FIXTURES="$SCRIPT_DIR/test-fixtures/wiki-lint"
LINT="$SCRIPT_DIR/wiki-lint.sh"

# Farben
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

PASS=0
FAIL=0

assert_exit() {
    # $1 = expected exit code, $2 = actual, $3 = test name
    local expected=$1
    local actual=$2
    local name=$3
    if [ "$actual" -eq "$expected" ]; then
        echo -e "${GREEN}✓${NC} $name (exit=$actual)"
        PASS=$((PASS+1))
    else
        echo -e "${RED}✗${NC} $name (expected exit=$expected, got=$actual)"
        FAIL=$((FAIL+1))
    fi
}

assert_report_contains() {
    # $1 = report file, $2 = expected substring, $3 = test name
    local report=$1
    local needle=$2
    local name=$3
    if grep -qF "$needle" "$report"; then
        echo -e "${GREEN}✓${NC} $name (found: '$needle')"
        PASS=$((PASS+1))
    else
        echo -e "${RED}✗${NC} $name (NOT found: '$needle')"
        FAIL=$((FAIL+1))
    fi
}

assert_report_not_contains() {
    local report=$1
    local needle=$2
    local name=$3
    if ! grep -qF "$needle" "$report"; then
        echo -e "${GREEN}✓${NC} $name (correctly absent: '$needle')"
        PASS=$((PASS+1))
    else
        echo -e "${RED}✗${NC} $name (UNEXPECTEDLY present: '$needle')"
        FAIL=$((FAIL+1))
    fi
}

run_lint() {
    # $1 = fixture dir, $2 = report file
    "$LINT" "$1" "$2" >/dev/null 2>&1
    return $?
}

echo "=== Test 1: clean-wiki (expect: exit 0, 0 issues)==="
REPORT=$(mktemp)
run_lint "$FIXTURES/clean-wiki" "$REPORT"
assert_exit 0 $? "clean-wiki exit code"
assert_report_contains "$REPORT" "Total Wiki-Pages: 3" "clean-wiki total count"
assert_report_contains "$REPORT" "✅ **0 / 3 (0%)**" "clean-wiki no stubs"
assert_report_contains "$REPORT" "✅ **3 / 3 present**" "clean-wiki all frontmatter present"
assert_report_contains "$REPORT" "✅ **0 broken**" "clean-wiki no broken refs"
assert_report_contains "$REPORT" "✅ **0 orphans**" "clean-wiki no orphans"
rm -f "$REPORT"

echo ""
echo "=== Test 2: messy-wiki (expect: exit 1, issues detected)==="
REPORT=$(mktemp)
run_lint "$FIXTURES/messy-wiki" "$REPORT"
EXIT=$?
assert_exit 1 $EXIT "messy-wiki exit code (issues found)"
assert_report_contains "$REPORT" "Stub-Quote" "messy-wiki has stub section"
assert_report_contains "$REPORT" "polymarket" "messy-wiki polymarket cluster detected"
assert_report_contains "$REPORT" "❌" "messy-wiki has at least one failure indicator"
rm -f "$REPORT"

echo ""
echo "=== Test 3: broken-wiki (expect: exit 1, multiple issues)==="
REPORT=$(mktemp)
run_lint "$FIXTURES/broken-wiki" "$REPORT"
EXIT=$?
assert_exit 1 $EXIT "broken-wiki exit code"
assert_report_contains "$REPORT" "broken" "broken-wiki broken refs detected"
assert_report_contains "$REPORT" "Missing" "broken-wiki missing frontmatter"
assert_report_contains "$REPORT" "orphan" "broken-wiki orphans detected"
rm -f "$REPORT"

echo ""
echo "=== Test 4: real repo (smoke-test, exit 0=clean OR 1=issues, NEVER 2)==="
REPORT=$(mktemp)
run_lint "$REPO_ROOT" "$REPORT"
EXIT=$?
# Smoke-test: exit 0 (clean state nach WMT-002) oder 1 (legitime Issues) ist OK.
# Nur exit 2 (Script-Crash) ist fail.
if [ "$EXIT" -eq 0 ] || [ "$EXIT" -eq 1 ]; then
    echo -e "${GREEN}✓${NC} real-repo exit code (exit=$EXIT, ok: 0 or 1)"
    PASS=$((PASS+1))
else
    echo -e "${RED}✗${NC} real-repo exit code (expected 0 or 1, got $EXIT — Script-Crash?)"
    FAIL=$((FAIL+1))
fi
# Section heißt jetzt "Frontmatter Präsenz" (mit Umlaut) — case-sensitive nach Umlaut suchen
assert_report_contains "$REPORT" "Frontmatter Präsenz" "real-repo frontmatter section (Schema v1.5)"
assert_report_contains "$REPORT" "Stub-Quote" "real-repo stub section"
# Generische Sanity: Summary-Linie existiert (egal ob Issues oder nicht)
assert_report_contains "$REPORT" "Total Wiki-Pages:" "real-repo summary line present"
rm -f "$REPORT"

echo ""
echo "=============================="
echo -e "Passed: ${GREEN}$PASS${NC}"
echo -e "Failed: ${RED}$FAIL${NC}"
echo "=============================="

if [ "$FAIL" -gt 0 ]; then
    exit 1
fi
exit 0
