name: Add Contributors

on:
  pull_request_target:
    types: [closed]
  issues:
    types: [closed]
  push:
    branches:
      - main
  workflow_dispatch:  # Allow manual triggering

permissions:
  contents: write
  pull-requests: write

jobs:
  add-contributors:
    runs-on: ubuntu-latest
    if: github.event_name == 'workflow_dispatch' || github.event_name == 'push' || (github.event_name == 'pull_request_target' && github.event.pull_request.merged == true)
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          fetch-depth: 0
          token: ${{ secrets.GITHUB_TOKEN }}
          ref: main  # Always checkout main branch for security

      - name: Update contributors
        uses: akhilmhdh/contributors-readme-action@v2.3.6
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          image_size: 100
          columns_per_row: 6
          collaborators: 'all'
          committer_username: 'github-actions[bot]'
          committer_email: '41898282+github-actions[bot]@users.noreply.github.com'
          commit_message: 'docs: update contributors list'
          readme_path: 'CONTRIBUTORS.md'
