#!/usr/bin/env bash
# Pre-push drift checks:
#  1. openapi.json in repo vs live production spec.
#  2. src/sonzai/_generated/models.py vs what datamodel-codegen would
#     produce from the committed openapi.json.
#  3. src/sonzai/_generated/resources/ vs what the Jinja resource
#     generator (scripts/codegen/generate_resources.py) would produce
#     from the committed openapi.json.
#
# Skip with `git push --no-verify`.

set -e

SPEC_URL="${OPENAPI_SPEC_URL:-https://api.sonz.ai/docs/openapi.json}"
LOCAL_SPEC="openapi.json"

# --- Check 1: live spec drift ----------------------------------------------

if [ -f "$LOCAL_SPEC" ]; then
  LIVE_SPEC="$(mktemp)"
  trap 'rm -f "$LIVE_SPEC"' EXIT

  if curl -sfL --max-time 10 "$SPEC_URL" -o "$LIVE_SPEC"; then
    if ! diff -q "$LOCAL_SPEC" "$LIVE_SPEC" > /dev/null 2>&1; then
      echo ""
      echo "⚠️  OpenAPI spec has drifted from production."
      echo ""
      echo "   Committed: $LOCAL_SPEC ($(wc -c < "$LOCAL_SPEC" | tr -d ' ') bytes)"
      echo "   Live:      $SPEC_URL ($(wc -c < "$LIVE_SPEC" | tr -d ' ') bytes)"
      echo ""
      echo "   → Run 'just sync-spec' to pull the latest spec."
      echo "   → Review the diff, update SDK types if needed, and re-push."
      echo "   → To bypass: git push --no-verify"
      echo ""
      exit 1
    fi
  else
    echo "⚠ Could not fetch $SPEC_URL — skipping live-spec drift check."
  fi
fi

# --- Check 2: regenerate and compare --------------------------------------

if [ -f "src/sonzai/_generated/models.py" ]; then
  TMP_OUT="$(mktemp -d)"
  trap 'rm -rf "$TMP_OUT" "$LIVE_SPEC"' EXIT

  if ! uv run --extra dev datamodel-codegen \
      --input openapi.json \
      --input-file-type openapi \
      --output "$TMP_OUT/models.py" \
      --output-model-type pydantic_v2.BaseModel \
      --target-python-version 3.11 \
      --snake-case-field \
      --use-annotated \
      --use-standard-collections \
      --use-union-operator \
      --use-field-description \
      --allow-population-by-field-name \
      --enum-field-as-literal all \
      --collapse-root-models \
      --use-schema-description \
      --disable-timestamp \
      > /dev/null 2>&1; then
    echo "⚠ Regenerator failed — skipping regen-drift check."
    exit 0
  fi

  if ! diff -q "src/sonzai/_generated/models.py" "$TMP_OUT/models.py" > /dev/null 2>&1; then
    echo ""
    echo "⚠️  _generated/models.py is out of sync with openapi.json."
    echo ""
    echo "   → Run 'just regenerate-sdk' and commit the result."
    echo "   → To bypass: git push --no-verify"
    echo ""
    exit 1
  fi
fi

# --- Check 3: regenerate resources and compare ----------------------------

if [ -d "src/sonzai/_generated/resources" ]; then
  TMP_RES="$(mktemp -d)"
  trap 'rm -rf "$TMP_OUT" "$LIVE_SPEC" "$TMP_RES"' EXIT

  if ! uv run --extra dev python -m scripts.codegen.generate_resources \
      openapi.json "$TMP_RES" > /dev/null 2>&1; then
    echo "⚠ Resource generator failed — skipping regen-drift check."
    exit 0
  fi

  # Format tmp output same way `just regenerate-sdk` does, so diff compares apples-to-apples.
  uv run --extra dev ruff format "$TMP_RES" > /dev/null 2>&1 || true

  if ! diff -rq --exclude=__pycache__ "src/sonzai/_generated/resources" "$TMP_RES" > /dev/null 2>&1; then
    echo ""
    echo "⚠️  _generated/resources/ is out of sync with openapi.json."
    echo ""
    echo "   → Run 'just regenerate-sdk' and commit the result."
    echo "   → To bypass: git push --no-verify"
    echo ""
    exit 1
  fi
fi

exit 0
