name: Flotpane Activity Monitor

on:
  push:
  issues:
    types: [opened, edited, deleted, closed, reopened]
  pull_request:
    types: [opened, edited, closed, reopened]
  issue_comment:
    types: [created, deleted]
  fork:
  watch:
    types: [started] # This triggers when someone stars the repo
  discussion:
    types: [created, answered]

jobs:
  discord-audit-log:
    name: Send Discord Audit Log
    runs-on: ubuntu-latest
    steps:
      - name: Parse Event and Send Webhook
        env:
          DISCORD_WEBHOOK_URL: ${{ secrets.DISCORD_WEBHOOK_URL_AUDIT_LOG }}
        run: |
          EVENT_NAME="${{ github.event_name }}"
          ACTOR="${{ github.actor }}"
          REPO="${{ github.repository }}"
          ACTION="${{ github.event.action }}"

          # Default fallback URL
          URL="https://github.com/$REPO"

          # Customize Title and URL based on the specific event
          if [ "$EVENT_NAME" == "issues" ]; then
            TITLE="🐛 Issue $ACTION in $REPO"
            URL="${{ github.event.issue.html_url }}"
          elif [ "$EVENT_NAME" == "pull_request" ]; then
            TITLE="🔄 Pull Request $ACTION in $REPO"
            URL="${{ github.event.pull_request.html_url }}"
          elif [ "$EVENT_NAME" == "push" ]; then
            TITLE="⬆️ Code Pushed to $REPO"
            URL="${{ github.event.compare }}" # Links to the commit comparison
          elif [ "$EVENT_NAME" == "issue_comment" ]; then
            TITLE="💬 Comment $ACTION on an Issue/PR"
            URL="${{ github.event.comment.html_url }}"
          elif [ "$EVENT_NAME" == "watch" ]; then
            TITLE="⭐ Repo Starred!"
          elif [ "$EVENT_NAME" == "fork" ]; then
            TITLE="🍴 Repo Forked!"
          elif [ "$EVENT_NAME" == "discussion" ]; then
            TITLE="🗣️ Discussion $ACTION"
            URL="${{ github.event.discussion.html_url }}"
          else
            TITLE="🔔 Activity ($EVENT_NAME) in $REPO"
          fi

          # Use jq to safely build the JSON payload (prevents special characters from breaking the script)
          # Color is set to 0 (Pitch Black) to match the Flotpane branding
          jq -n \
            --arg title "$TITLE" \
            --arg url "$URL" \
            --arg desc "**User:** \`$ACTOR\` triggered \`$EVENT_NAME\`.\n\n[**View this activity on GitHub**]($url)" \
            '{
              username: "Flotpane Monitor",
              avatar_url: "https://github.githubassets.com/images/modules/logos_page/GitHub-Mark.png",
              embeds: [{
                title: $title,
                url: $url,
                description: $desc,
                color: 0
              }]
            }' | curl -H "Content-Type: application/json" -d @- $DISCORD_WEBHOOK_URL
