name: Check Duplicate Issues

on:
  issues:
    types: [opened, edited]

jobs:
  check-duplicate:
    runs-on: ubuntu-latest
    permissions:
      issues: write
      contents: read
    steps:
      - name: Check for potential duplicates
        uses: wow-actions/potential-duplicates@v1
        with:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          label: potential-duplicate
          threshold: 0.6
          reaction: eyes
          comment: |
            **⚠️ Potential duplicate detected**

            This issue appears similar to existing issue(s):
            {{#issues}}
            - [#{{number}}]({{html_url}}) - {{title}} ({{accuracy}}% similar)
            {{/issues}}

            Please review the linked issue(s) to see if they address your concern. If this is not a duplicate, please provide additional context to help us understand the difference.

      - name: Checkout close script
        if: github.event.action == 'opened'
        uses: actions/checkout@v4
        with:
          sparse-checkout: .github/scripts

      - name: Set up Python
        if: github.event.action == 'opened'
        uses: actions/setup-python@v5
        with:
          python-version: "3.11"

      - name: Auto-close if high-confidence duplicate
        if: github.event.action == 'opened'
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          python3 .github/scripts/close_duplicate_issues.py \
            --issue-number ${{ github.event.issue.number }} \
            --repo ${{ github.repository }} \
            --threshold 0.85 \
            --close
