#!/usr/bin/env bash
# End-to-end: customer hires worker, completes job, submits review.
set -euo pipefail

API="${API_URL:-http://localhost:4000/api}"
PASS="${DEMO_PASSWORD:-Password123!}"

login() {
  curl -s -X POST "$API/auth/login" \
    -H 'Content-Type: application/json' \
    -d "{\"identifier\":\"$1\",\"password\":\"$PASS\"}" \
    | python3 -c "import sys,json; d=json.load(sys.stdin); assert d.get('success'); print(d['data']['accessToken'])"
}

echo "=== E2E: hire → complete → review ==="

MARIA_TOKEN=$(login "maria@example.com")
MARCO_TOKEN=$(login "marco@worker.com")
CAT=$(curl -s "$API/categories?limit=1" | python3 -c "import sys,json; print(json.load(sys.stdin)['data'][0]['id'])")

JOB=$(curl -s -X POST "$API/jobs" \
  -H "Authorization: Bearer $MARIA_TOKEN" \
  -H 'Content-Type: application/json' \
  -d "{\"title\":\"E2E Review Test $(date +%s)\",\"description\":\"Automated review flow test\",\"categoryId\":\"$CAT\",\"budget\":1500,\"location\":\"Davao City\"}" \
  | python3 -c "import sys,json; d=json.load(sys.stdin); assert d.get('success'); print(d['data']['id'])")
echo "Job created: $JOB"

REASON=$(curl -s "$API/reviews/eligible/$JOB" -H "Authorization: Bearer $MARIA_TOKEN" \
  | python3 -c "import sys,json; print(json.load(sys.stdin)['data']['reason'])")
echo "Before hire: $REASON"

QUOTE=$(curl -s -X POST "$API/quotations" \
  -H "Authorization: Bearer $MARCO_TOKEN" \
  -H 'Content-Type: application/json' \
  -d "{\"jobId\":\"$JOB\",\"amount\":1400,\"message\":\"I can do this job\"}" \
  | python3 -c "import sys,json; d=json.load(sys.stdin); assert d.get('success'); print(d['data']['id'])")
echo "Quote submitted: $QUOTE"

curl -s -X PATCH "$API/quotations/$QUOTE/accept" \
  -H "Authorization: Bearer $MARIA_TOKEN" | python3 -c "import sys,json; d=json.load(sys.stdin); assert d.get('success')"
echo "Quote accepted (worker hired)"

REASON=$(curl -s "$API/reviews/eligible/$JOB" -H "Authorization: Bearer $MARIA_TOKEN" \
  | python3 -c "import sys,json; r=json.load(sys.stdin)['data']; print(r.get('reason') or 'ELIGIBLE=' + str(r.get('eligible')))")
echo "After hire, before complete: $REASON"
[[ "$REASON" == *"completed"* ]] || { echo "FAIL: expected completed message"; exit 1; }

curl -s -X PATCH "$API/jobs/$JOB/status" \
  -H "Authorization: Bearer $MARIA_TOKEN" \
  -H 'Content-Type: application/json' \
  -d '{"status":"COMPLETED"}' | python3 -c "import sys,json; d=json.load(sys.stdin); assert d.get('success')"
echo "Job marked completed"

ELIG=$(curl -s "$API/reviews/eligible/$JOB" -H "Authorization: Bearer $MARIA_TOKEN" \
  | python3 -c "import sys,json; d=json.load(sys.stdin)['data']; assert d['eligible']; print(d['workerName'])")
echo "Eligible to review: $ELIG"

REVIEW=$(curl -s -X POST "$API/reviews" \
  -H "Authorization: Bearer $MARIA_TOKEN" \
  -H 'Content-Type: application/json' \
  -d "{\"jobId\":\"$JOB\",\"qualityRating\":5,\"communicationRating\":4,\"punctualityRating\":5,\"comment\":\"Great work on E2E test\"}" \
  | python3 -c "import sys,json; d=json.load(sys.stdin); assert d.get('success'); print(d['data']['id'])")
echo "Review submitted: $REVIEW"

# Duplicate blocked
curl -s -X POST "$API/reviews" \
  -H "Authorization: Bearer $MARIA_TOKEN" \
  -H 'Content-Type: application/json' \
  -d "{\"jobId\":\"$JOB\",\"qualityRating\":5,\"communicationRating\":5,\"punctualityRating\":5}" \
  | python3 -c "import sys,json; d=json.load(sys.stdin); assert not d.get('success'); assert 'already' in d.get('error','').lower()"

echo "Duplicate review correctly blocked"
echo "=== E2E PASSED ==="
