name: Monthly Release Check

on:
  schedule:
    # Run at 09:00 UTC on the first of each month
    - cron: "0 9 1 * *"
  workflow_dispatch: # Allow manual trigger

permissions:
  contents: write
  pull-requests: write

jobs:
  trigger-release:
    name: Trigger release if deps updated
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v6
        with:
          fetch-depth: 0

      - name: Check for dependency updates since last tag
        id: check
        run: |
          LAST_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "")
          if [ -z "$LAST_TAG" ]; then
            echo "No tags found, skipping"
            echo "has_updates=false" >> "$GITHUB_OUTPUT"
            exit 0
          fi

          # Check for chore(deps) commits since last tag
          DEPS_COMMITS=$(git log "$LAST_TAG"..HEAD --oneline --grep="chore(deps)" | wc -l)

          if [ "$DEPS_COMMITS" -gt 0 ]; then
            echo "Found $DEPS_COMMITS dependency update(s) since $LAST_TAG"
            echo "has_updates=true" >> "$GITHUB_OUTPUT"
          else
            echo "No dependency updates since $LAST_TAG"
            echo "has_updates=false" >> "$GITHUB_OUTPUT"
          fi

      - name: Create release trigger PR
        if: steps.check.outputs.has_updates == 'true'
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          BRANCH="monthly-deps-release-$(date +%Y%m)"

          # Check if branch/PR already exists
          if gh pr list --head "$BRANCH" --state open | grep -q .; then
            echo "PR already exists for this month, skipping"
            exit 0
          fi

          git config user.name "github-actions[bot]"
          git config user.email "github-actions[bot]@users.noreply.github.com"

          git checkout -b "$BRANCH"
          git commit --allow-empty -m "fix: monthly dependency update release"
          git push -u origin "$BRANCH"

          gh pr create \
            --title "fix: monthly dependency update release" \
            --body "Automated monthly release to publish accumulated dependency updates." \
            --base main \
            --head "$BRANCH"
