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

BASE_URL="${BASE_URL:-http://127.0.0.1:8080}"
CALDAV_USER="${CALDAV_USER:-rw_user@example.test}"
CALDAV_PASSWORD="${CALDAV_PASSWORD:-rwpass123456}"

require_contains() {
  local haystack="$1"
  local needle="$2"
  local msg="$3"
  if ! printf '%s' "$haystack" | grep -q "$needle"; then
    echo "[caldav-compat] FAIL: $msg"
    exit 1
  fi
}

require_contains_ci() {
  local haystack="$1"
  local needle="$2"
  local msg="$3"
  if ! printf '%s' "$haystack" | grep -qi "$needle"; then
    echo "[caldav-compat] FAIL: $msg"
    exit 1
  fi
}

echo "[caldav-compat] checking unauthenticated challenge"
unauth_headers="$(curl -sS -i "$BASE_URL/caldav/" | tr -d '\r')"
require_contains "$unauth_headers" ' 401 ' "expected 401 from /caldav/"
require_contains_ci "$unauth_headers" 'www-authenticate: basic' "missing WWW-Authenticate Basic challenge"

echo "[caldav-compat] checking OPTIONS capability advertisement"
opt_headers="$(curl -sS -i -u "$CALDAV_USER:$CALDAV_PASSWORD" -X OPTIONS "$BASE_URL/caldav/" | tr -d '\r')"
require_contains_ci "$opt_headers" 'dav:' "missing DAV header"
require_contains_ci "$opt_headers" 'calendar-access' "missing DAV calendar-access advertisement"
require_contains_ci "$opt_headers" 'allow: ' "missing Allow header for CalDAV OPTIONS"
require_contains_ci "$opt_headers" 'options' "Allow header missing OPTIONS"
require_contains_ci "$opt_headers" 'propfind' "Allow header missing PROPFIND"
require_contains_ci "$opt_headers" 'report' "Allow header missing REPORT"

echo "[caldav-compat] checking root PROPFIND discovery"
root_xml="$(curl -fsS -u "$CALDAV_USER:$CALDAV_PASSWORD" -X PROPFIND \
  -H 'Depth: 1' \
  -H 'Content-Type: application/xml' \
  --data '<D:propfind xmlns:D="DAV:"><D:prop><D:current-user-principal/><D:resourcetype/><D:displayname/></D:prop></D:propfind>' \
  "$BASE_URL/caldav/")"
require_contains "$root_xml" '<D:current-user-principal>' "root discovery missing current-user-principal"
require_contains "$root_xml" '/caldav/calendars/public/' "root discovery missing public calendar href"
require_contains "$root_xml" '<C:calendar/>' "root discovery missing calendar collection marker"

principal_href="$(printf '%s' "$root_xml" | grep -oE '/caldav/principals/[^<"]+/' | head -n 1 || true)"
if [ -z "$principal_href" ]; then
  echo "[caldav-compat] FAIL: unable to locate principal href in root PROPFIND"
  exit 1
fi

echo "[caldav-compat] checking principal calendar-home-set"
principal_xml="$(curl -fsS -u "$CALDAV_USER:$CALDAV_PASSWORD" -X PROPFIND \
  -H 'Depth: 0' \
  -H 'Content-Type: application/xml' \
  --data '<D:propfind xmlns:D="DAV:" xmlns:C="urn:ietf:params:xml:ns:caldav"><D:prop><C:calendar-home-set/></D:prop></D:propfind>' \
  "$BASE_URL$principal_href")"
require_contains "$principal_xml" '<C:calendar-home-set>' "principal missing calendar-home-set"
require_contains "$principal_xml" '/caldav/calendars/' "calendar-home-set does not point to /caldav/calendars/"

echo "[caldav-compat] checking calendars home-set collection listing"
home_xml="$(curl -fsS -u "$CALDAV_USER:$CALDAV_PASSWORD" -X PROPFIND \
  -H 'Depth: 1' \
  -H 'Content-Type: application/xml' \
  --data '<D:propfind xmlns:D="DAV:" xmlns:C="urn:ietf:params:xml:ns:caldav"><D:prop><D:displayname/><D:resourcetype/><C:supported-calendar-component-set/></D:prop></D:propfind>' \
  "$BASE_URL/caldav/calendars/")"
require_contains "$home_xml" '/caldav/calendars/public/' "calendar home-set listing missing public calendar href"
require_contains "$home_xml" '<C:calendar/>' "calendar home-set listing missing calendar marker"
require_contains "$home_xml" '<C:comp name="VEVENT"/>' "calendar home-set listing missing supported component set"

echo "[caldav-compat] all checks passed"
