#!/usr/bin/env bash
# 本番環境ヘルスチェック
# 使い方: bash scripts/check_prod.sh [username] [password]
# 例:     bash scripts/check_prod.sh claude keina1234

BASE="https://main.keinafarm.net"
USER="${1:-claude}"
PASS="${2:-keina1234}"

OK=0
NG=0

check() {
  local label="$1"
  local expected="$2"
  local actual="$3"
  if [ "$actual" = "$expected" ]; then
    echo "  PASS  $label (HTTP $actual)"
    OK=$((OK+1))
  else
    echo "  FAIL  $label (expected $expected, got $actual)"
    NG=$((NG+1))
  fi
}

echo "=== 本番ヘルスチェック: $BASE ==="
echo "--- $(date '+%Y-%m-%d %H:%M:%S') ---"
echo ""

# 1. フロントエンド到達確認
echo "[1] フロントエンド"
STATUS=$(curl -s -o /dev/null -w "%{http_code}" --max-time 10 "$BASE/")
check "トップページ" "200" "$STATUS"

STATUS=$(curl -s -o /dev/null -w "%{http_code}" --max-time 10 "$BASE/login")
check "ログインページ" "200" "$STATUS"

# 2. バックエンドAPI（認証不要）
echo ""
echo "[2] バックエンドAPI（認証なし）"
STATUS=$(curl -s -o /dev/null -w "%{http_code}" --max-time 10 "$BASE/api/auth/jwt/create/")
check "認証エンドポイント到達" "405" "$STATUS"  # GETは405が正常

# 3. JWT取得
echo ""
echo "[3] JWT認証"
TOKEN_RESP=$(curl -s --max-time 10 -X POST "$BASE/api/auth/jwt/create/" \
  -H "Content-Type: application/json" \
  -d "{\"username\":\"$USER\",\"password\":\"$PASS\"}")
ACCESS=$(echo "$TOKEN_RESP" | grep -o '"access":"[^"]*"' | cut -d'"' -f4)

if [ -n "$ACCESS" ]; then
  echo "  PASS  JWTトークン取得"
  OK=$((OK+1))
else
  echo "  FAIL  JWTトークン取得失敗 (レスポンス: $TOKEN_RESP)"
  NG=$((NG+1))
  echo ""
  echo "=== 結果: PASS $OK / FAIL $NG (認証失敗のため以降の確認をスキップ) ==="
  exit 1
fi

# 4. 認証が必要なAPI
echo ""
echo "[4] 認証済みAPI"
AUTH="Authorization: Bearer $ACCESS"

STATUS=$(curl -s -o /dev/null -w "%{http_code}" --max-time 10 -H "$AUTH" "$BASE/api/fields/")
check "圃場一覧 API" "200" "$STATUS"

STATUS=$(curl -s -o /dev/null -w "%{http_code}" --max-time 10 -H "$AUTH" "$BASE/api/plans/crops/")
check "品種一覧 API" "200" "$STATUS"

STATUS=$(curl -s -o /dev/null -w "%{http_code}" --max-time 10 -H "$AUTH" "$BASE/api/fertilizer/fertilizers/")
check "肥料マスタ API" "200" "$STATUS"

STATUS=$(curl -s -o /dev/null -w "%{http_code}" --max-time 10 -H "$AUTH" "$BASE/api/fertilizer/plans/?year=2026")
check "施肥計画一覧 API" "200" "$STATUS"

STATUS=$(curl -s -o /dev/null -w "%{http_code}" --max-time 10 -H "$AUTH" "$BASE/api/weather/summary/?year=2025")
check "気象データサマリー API" "200" "$STATUS"

# 5. 結果サマリー
echo ""
TOTAL=$((OK+NG))
if [ $NG -eq 0 ]; then
  echo "=== 結果: 全 $TOTAL 項目 PASS ==="
else
  echo "=== 結果: PASS $OK / FAIL $NG (合計 $TOTAL) ==="
  exit 1
fi
