name: Create Issues from Drafts

on:
  workflow_dispatch:
    inputs:
      dry_run:
        description: 'Dry run mode (preview without creating issues)'
        required: false
        type: boolean
        default: true

permissions:
  contents: write
  issues: write

jobs:
  create-issues:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v5
        with:
          fetch-depth: 0

      - name: Setup Node.js
        # Read the committed Node pin rather than a hardcoded literal, so this
        # non-site workflow tracks the same version as the site CI (.nvmrc = 24;
        # family Astro site standard 14.8 uses node-version-file, not a literal).
        uses: actions/setup-node@v6
        with:
          node-version-file: .nvmrc

      - name: Install dependencies
        working-directory: .github/scripts
        run: npm install

      - name: Create issues from drafts
        working-directory: .github/scripts
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          DRY_RUN: ${{ inputs.dry_run }}
        run: node create-issues.js

      - name: Commit and push changes
        if: ${{ !inputs.dry_run }}
        run: |
          git config --local user.email "github-actions[bot]@users.noreply.github.com"
          git config --local user.name "github-actions[bot]"
          git add .
          if git diff --staged --quiet; then
            echo "No changes to commit"
          else
            git commit -m "chore: process issue drafts - create issues and archive files"
            git push
          fi
