name: check-outdated-dependencies
permissions:
  contents: read

on:
  push: # This will trigger the action when a pull request is opened or updated.
    branches:
      - "release/**" # This will trigger the action when any branch starting with "release/" is created.
  workflow_dispatch: # Allow manual triggering if needed.

jobs:
  backend:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          fetch-tags: true
          fetch-depth: 0
          persist-credentials: false
      - uses: ./.github/actions/setup_build_env
        with:
          python-version: "3.13"
          run-uv-sync: true

      - name: Check outdated backend dependencies
        run: |
          outdated=$(uv pip list --outdated)
          echo "Outdated:"
          echo "$outdated"

          filtered_outdated=$(echo "$outdated" | grep -vE 'pyright|ruff' || true)

          if [ ! -z "$filtered_outdated" ]; then
            echo "Outdated dependencies found:"
            echo "$filtered_outdated"
            exit 1
          else
            echo "All dependencies are up to date. (pyright and ruff are ignored)"
          fi

  frontend:
    runs-on: ubuntu-latest
    env:
      # reflex-enterprise restricts `reflex run --env prod` to paid tiers but
      # exempts reflex's own integration tests via the app harness flag.
      APP_HARNESS_FLAG: "true"

    steps:
      - name: Checkout code
        uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          fetch-tags: true
          fetch-depth: 0
          persist-credentials: false

      - uses: ./.github/actions/setup_build_env
        with:
          python-version: "3.13"
      - name: Install Bun
        uses: oven-sh/setup-bun@0c5077e51419868618aeaa5fe8019c62421857d6 # v2.2.0

      - name: Install Requirements for reflex-docs
        working-directory: ./docs/app
        run: uv sync
      - name: Init Website for reflex-docs
        working-directory: ./docs/app
        run: uv run --active --no-sync reflex init
      - name: Run Website and Check for errors
        run: |
          uv run --active --no-sync bash scripts/integration.sh ./docs/app prod
      - name: Check outdated frontend dependencies
        working-directory: ./docs/app/.web
        run: |
          raw_outdated=$(bun outdated)
          outdated=$(echo "$raw_outdated" | grep -vE '\|\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\|' || true)
          echo "Outdated:"
          echo "$outdated"

          # Ignore 3rd party dependencies that are not updated.
          filtered_outdated=$(echo "$outdated" | grep -vE 'Package|@chakra-ui|lucide-react|@splinetool/runtime|ag-grid-react|framer-motion|ag-grid' || true)
          no_extra=$(echo "$filtered_outdated" | grep -vE '\|\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-' || true)


          if [ ! -z "$no_extra" ]; then
            echo "Outdated dependencies found:"
            echo "$filtered_outdated"
            exit 1
          else
            echo "All dependencies are up to date. (3rd party packages are ignored)"
          fi
