name: Post Announcements to Slack

on:
  discussion:
    types: [created, edited]

jobs:
  notify-slack:
    runs-on: ubuntu-latest
    steps:
      - name: Check if announcement
        id: check
        run: |
          # Check if the discussion is in the announcements category
          CATEGORY="${{ github.event.discussion.category.slug }}"
          echo "Category: $CATEGORY"
          
          # Check for both possible variations
          if [ "$CATEGORY" = "announcements" ] || [ "$CATEGORY" = "announcement" ]; then
            echo "is_announcement=true" >> $GITHUB_OUTPUT
          else
            echo "is_announcement=false" >> $GITHUB_OUTPUT
          fi
          
      - name: Checkout repository
        if: steps.check.outputs.is_announcement == 'true'
        uses: actions/checkout@v4

      - name: Prepare Slack Message
        if: steps.check.outputs.is_announcement == 'true'
        id: prepare_message
        run: |
          # Make script executable and run it
          chmod +x ./scripts/prepare-slack-message.sh
          PAYLOAD=$(./scripts/prepare-slack-message.sh)
          
          # Set output for next step
          echo "slack_payload<<EOF" >> $GITHUB_OUTPUT
          echo "$PAYLOAD" >> $GITHUB_OUTPUT
          echo "EOF" >> $GITHUB_OUTPUT
        env:
          DISCUSSION_TITLE: ${{ github.event.discussion.title }}
          DISCUSSION_BODY: ${{ github.event.discussion.body }}
          DISCUSSION_URL: ${{ github.event.discussion.html_url }}
      - name: Post to Slack
        if: steps.check.outputs.is_announcement == 'true'
        uses: 8398a7/action-slack@v3
        with:
          status: custom
          custom_payload: |
            ${{ steps.prepare_message.outputs.slack_payload }}
        env:
          SLACK_WEBHOOK_URL: ${{ secrets.SLACK_DOGRAH_COMMUNITY_WEBHOOK_URL }}
