name: Close Inactive Issues
on:
  schedule:
    - cron: "30 1 * * *"
  issues:
    types: [labeled]

jobs:
  notify-on-label:
    if: github.event_name == 'issues' && github.event.label.name == 'autoclose'
    runs-on: ubuntu-latest
    permissions:
      issues: write
    steps:
      - name: Comment on autoclose label
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          GH_REPO: ${{ github.repository }}
        run: gh issue comment ${{ github.event.issue.number }} --body "This issue has been marked for autoclose and will be closed after 14 days of inactivity."

  close-issues:
    if: github.event_name == 'schedule'
    runs-on: ubuntu-latest
    permissions:
      issues: write
      pull-requests: write
    steps:
      - uses: actions/stale@v5
        with:
          only-labels: autoclose
          days-before-issue-stale: 7
          days-before-issue-close: 7
          stale-issue-label: "stale"
          stale-issue-message: "This issue is stale because it has been open for 7 days with no activity."
          close-issue-message: "This issue was closed because it has been inactive for 7 days since being marked as stale."
          days-before-pr-stale: -1
          days-before-pr-close: -1
          repo-token: ${{ secrets.GITHUB_TOKEN }}
