#!/usr/bin/env bash
set -euo pipefail

BASE_URL="${1:-http://127.0.0.1:8000}"
TIMEOUT_SECONDS="${TIMEOUT_SECONDS:-12}"

pass_count=0
fail_count=0

log_pass() {
  pass_count=$((pass_count + 1))
  printf 'PASS %s\n' "$1"
}

log_fail() {
  fail_count=$((fail_count + 1))
  printf 'FAIL %s\n' "$1"
}

request_status() {
  local path="$1"
  curl -sS -L -o /tmp/lineup_smoke_body.$$ \
    -w "%{http_code}" \
    --max-time "$TIMEOUT_SECONDS" \
    "$BASE_URL$path" || true
}

request_status_no_follow() {
  local path="$1"
  curl -sS -o /tmp/lineup_smoke_body.$$ \
    -w "%{http_code}" \
    --max-time "$TIMEOUT_SECONDS" \
    "$BASE_URL$path" || true
}

assert_status() {
  local label="$1"
  local got="$2"
  local expected="$3"
  if [[ "$got" == "$expected" ]]; then
    log_pass "$label (status=$got)"
  else
    log_fail "$label (expected=$expected, got=$got)"
  fi
}

assert_body_contains() {
  local label="$1"
  local needle="$2"
  if grep -Fq "$needle" /tmp/lineup_smoke_body.$$; then
    log_pass "$label"
  else
    log_fail "$label (missing '$needle')"
  fi
}

echo "== LineUp smoke test =="
echo "Base URL: $BASE_URL"
echo

status="$(request_status "/health")"
assert_status "GET /health" "$status" "200"
assert_body_contains "/health contains status ok" "\"status\":\"ok\""

status="$(request_status "/")"
assert_status "GET /" "$status" "200"
assert_body_contains "/ contains expected LineUp landing copy" "Deleguez votre attente"

status="$(request_status "/bo/login")"
assert_status "GET /bo/login" "$status" "200"
assert_body_contains "/bo/login contains auth block" "Authentification BO"

status="$(request_status "/images/lineup-logo.png")"
assert_status "GET /images/lineup-logo.png" "$status" "200"

status="$(request_status "/images/splash-screen-light.png")"
assert_status "GET /images/splash-screen-light.png" "$status" "200"

status="$(request_status_no_follow "/bo/dashboard")"
if [[ "$status" == "302" || "$status" == "301" ]]; then
  log_pass "GET /bo/dashboard redirects when unauthenticated (status=$status)"
else
  log_fail "GET /bo/dashboard unauthenticated redirect (expected=301/302, got=$status)"
fi

status="$(curl -sS -o /tmp/lineup_smoke_body.$$ -w "%{http_code}" \
  --max-time "$TIMEOUT_SECONDS" \
  -H "Accept: application/json" \
  "$BASE_URL/api/auth/me" || true)"
if [[ "$status" == "401" || "$status" == "403" ]]; then
  log_pass "GET /api/auth/me unauthorized without token (status=$status)"
else
  log_fail "GET /api/auth/me unauthorized without token (expected=401/403, got=$status)"
fi

status="$(curl -sS -o /tmp/lineup_smoke_body.$$ -w "%{http_code}" \
  --max-time "$TIMEOUT_SECONDS" \
  -H "Accept: application/json" \
  -H "Content-Type: application/json" \
  -d '{}' \
  "$BASE_URL/api/auth/login" || true)"
if [[ "$status" == "422" || "$status" == "400" ]]; then
  log_pass "POST /api/auth/login validation guard (status=$status)"
else
  log_fail "POST /api/auth/login validation guard (expected=400/422, got=$status)"
fi

# Infra guard: PATCH/DELETE must reach Laravel as-is.
# If Apache/proxy rewrites methods to GET, the body says "GET method ...", which is a prod blocker.
status="$(curl -sS -o /tmp/lineup_smoke_body.$$ -w "%{http_code}" \
  --max-time "$TIMEOUT_SECONDS" \
  -X PATCH \
  -H "Accept: application/json" \
  -H "Content-Type: application/json" \
  -d '{}' \
  "$BASE_URL/api/auth/login" || true)"
if [[ "$status" == "405" ]] && grep -Fq "The PATCH method is not supported for route" /tmp/lineup_smoke_body.$$; then
  log_pass "PATCH method passthrough on /api/auth/login (status=$status)"
else
  log_fail "PATCH method passthrough on /api/auth/login (expected=405 with PATCH marker, got status=$status)"
fi

status="$(curl -sS -o /tmp/lineup_smoke_body.$$ -w "%{http_code}" \
  --max-time "$TIMEOUT_SECONDS" \
  -X DELETE \
  -H "Accept: application/json" \
  "$BASE_URL/api/auth/login" || true)"
if [[ "$status" == "405" ]] && grep -Fq "The DELETE method is not supported for route" /tmp/lineup_smoke_body.$$; then
  log_pass "DELETE method passthrough on /api/auth/login (status=$status)"
else
  log_fail "DELETE method passthrough on /api/auth/login (expected=405 with DELETE marker, got status=$status)"
fi

echo
echo "Summary: $pass_count passed, $fail_count failed"
rm -f /tmp/lineup_smoke_body.$$

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