#!/bin/bash
set -e

echo "🚀 Starting PTM QA Automation Suite..."

# Always regenerate the manual + automated test-case workbook so it reflects
# the current scripts/build_test_cases_xlsx.py source of truth.
echo "📒 Regenerating data/test_cases.xlsx ..."
python3 scripts/build_test_cases_xlsx.py || echo "⚠️ Workbook build failed; continuing."

# Run tests to generate fresh results
if [ "$RUN_TESTS" = "true" ]; then
    echo "🧪 Cleaning previous results and running Playwright/Pytest suite..."
    rm -rf allure-results allure-report
    # Generate both Pytest-HTML and Allure results
    pytest --html=index.html --self-contained-html --alluredir=allure-results || echo "⚠️ Some tests failed, but continuing to serve reports."
fi

# Generate the static Allure report if results exist
if [ -d "allure-results" ]; then
    echo "📊 Writing Allure environment.properties ..."
    python3 scripts/write_allure_environment.py --runner dashboard || echo "⚠️ environment.properties write failed; continuing."

    echo "📊 Restoring prior Allure history for trend graphs ..."
    python3 scripts/restore_allure_history.py || echo "⚠️ history restore failed; continuing with fresh trends."

    echo "📊 Generating Allure report..."
    allure generate allure-results --clean -o allure-report
fi

# Fix nested allure-report/ from older pipeline builds (docker cp into existing dir).
if [ -f "allure-report/allure-report/index.html" ]; then
    echo "📊 Flattening nested allure-report/ layout..."
    mv allure-report/allure-report /tmp/allure-report-real
    rm -rf allure-report
    mv /tmp/allure-report-real allure-report
fi

# Ensure report trees exist so all dashboard ports bind (EKS readiness / ALB health).
mkdir -p allure-report data
if [ ! -f "allure-report/index.html" ]; then
    echo "⚠️ No Allure report in image; serving placeholder on :8081."
    cat > allure-report/index.html <<'EOF'
<!DOCTYPE html><html><head><meta charset="utf-8"><title>Allure report</title></head>
<body><p>Allure report not yet generated. Run the pipeline test step before build, or set RUN_TESTS=true.</p></body></html>
EOF
fi
if [ ! -f "index.html" ]; then
    echo "⚠️ No pytest-html report in image; serving placeholder on :9090."
    cat > index.html <<'EOF'
<!DOCTYPE html><html><head><meta charset="utf-8"><title>pytest-html</title></head>
<body><p>pytest-html report not yet generated. Run the pipeline test step before build, or set RUN_TESTS=true.</p></body></html>
EOF
fi
if [ ! -f "load-report.html" ]; then
    echo "⚠️ No Locust HTML report in image; serving placeholder on :8082."
    cat > /tmp/load-report.html <<'EOF'
<!DOCTYPE html><html><head><meta charset="utf-8"><title>Locust report</title></head>
<body><p>Locust HTML report not present in this image build.</p></body></html>
EOF
fi

# Unified port map (matches Dockerfile EXPOSE + docker-compose + Makefile report-serve):
#   9090 → pytest-html (index.html)
#   8081 → Allure static report
#   8082 → Locust HTML report (load-report.html)
#   8083 → Test-case workbook download (data/test_cases.xlsx)
#   8089 → Locust UI
#
# 1. Start Locust UI (Port 8089)
echo "🐜 Starting Locust UI on http://localhost:8089 ..."
locust -f tests/performance/locustfile.py --host "${API_BASE_URL:-http://127.0.0.1:8080/api}" > /tmp/locust.log 2>&1 &

# 2. Serve pytest-html report (Port 9090) — index.html lives at the project root
echo "🎭 Serving pytest-html on http://localhost:9090/index.html ..."
python3 scripts/no_cache_http_server.py 9090 --bind 0.0.0.0 --directory . &

# 3. Serve Allure static report (Port 8081)
echo "📈 Serving Allure on http://localhost:8081 ..."
python3 scripts/no_cache_http_server.py 8081 --bind 0.0.0.0 --directory allure-report &

# 4. Serve Locust HTML report (Port 8082) — load-report.html at project root
echo "📊 Serving Locust HTML report on http://localhost:8082/load-report.html ..."
python3 scripts/no_cache_http_server.py 8082 --bind 0.0.0.0 --directory /tmp &

# 5. Serve the test-case workbook (Port 8083)
echo "📒 Serving Test-Case workbook on http://localhost:8083/test_cases.xlsx ..."
python3 scripts/no_cache_http_server.py 8083 --bind 0.0.0.0 --directory data &

# Keep the container running
echo "✅ All dashboards are active. Press Ctrl+C to stop."
wait
