# API Plan Finder — Automated Weekly Drift & Model Deprecation Watch
# 1. Capture baseline once:
#    curl -sS -X POST https://plans.intoperson.com/api/v1/watch/baseline -H "Content-Type: application/json" \
#      -d '{"conditions":{"product_types":["tts_api"],"monthly_characters":2000000}}' > .plan-finder-baseline.json
# 2. Commit .plan-finder-baseline.json and save this file as .github/workflows/api-plan-watch.yml
name: API Plan Drift Watch

on:
  schedule:
    - cron: '0 9 * * 1'
  workflow_dispatch:

permissions:
  issues: write
  contents: read

jobs:
  check-api-plan-drift:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Check watched API plan against daily official-page rechecks
        id: watch
        env:
          PF_API_KEY: ${{ secrets.PLAN_FINDER_API_KEY }}
        run: |
          CURL_ARGS=(-sS -X POST "https://plans.intoperson.com/api/v1/watch/check" -H "Content-Type: application/json")
          if [ -n "$PF_API_KEY" ]; then CURL_ARGS+=(-H "Authorization: Bearer $PF_API_KEY"); fi
          curl "${CURL_ARGS[@]}" --data-binary @.plan-finder-baseline.json > watch-result.json
          STATUS=$(node -e "const r=JSON.parse(require('fs').readFileSync('watch-result.json','utf8')); console.log(r.status||'unknown')")
          echo "status=$STATUS" >> "$GITHUB_OUTPUT"
          if [ "$STATUS" = "broken" ]; then
            echo "::warning::API Plan Finder detected a pricing hike, condition break, or scheduled model shutdown."
            exit 1
          fi
