name: Update Relay Data

on:
  schedule:
    # Run every day at 6:00 AM UTC
    - cron: '0 6 * * *'
  workflow_dispatch: # Allows manual triggering

permissions:
  contents: write

jobs:
  update-relay-data:
    runs-on: ubuntu-latest
    
    steps:
    - name: Checkout repository
      uses: actions/checkout@v4
      with:
        token: ${{ secrets.GITHUB_TOKEN }}
    
    - name: Set up Python
      uses: actions/setup-python@v5
      with:
        python-version: '3.x'
    
    - name: Install Python dependencies
      run: |
        python -m pip install --upgrade pip
        pip install -r requirements.txt
    
    - name: Install required dependencies
      run: |
        sudo apt-get update && sudo apt-get install -y jq dnsutils gzip
        wget https://github.com/fiatjaf/nak/releases/download/v0.15.3/nak-v0.15.3-linux-amd64
        sudo mv nak-v0.15.3-linux-amd64 /usr/local/bin/nak
        sudo chmod +x /usr/local/bin/nak
        echo "Verifying nak installation:"
        nak --version
        which nak
    
    - name: Make scripts executable
      run: |
        chmod +x filter_bitchat_relays.sh
        chmod +x relays_geo_lookup.sh
    
    - name: Update relay discovery results
      run: |
        python3 nostr_relay_discovery.py wss://relay.damus.io --output relay_discovery_results.json --batch-size 20
    
    - name: Update relay geolocation data
      run: |
        cat relay_discovery_results.json | jq .functioning_relays[] | ./filter_bitchat_relays.sh | sed 's|^wss://||' | ./relays_geo_lookup.sh nostr_relays.csv
    
    - name: Check for changes
      id: git-check
      run: |
        git diff --exit-code || echo "changes=true" >> $GITHUB_OUTPUT
    
    - name: Commit and push changes
      if: steps.git-check.outputs.changes == 'true'
      run: |
        git config --local user.email "action@github.com"
        git config --local user.name "GitHub Action"
        git add relay_discovery_results.json nostr_relays.csv
        git commit -m "Automated update of relay data - $(date -u)"
        git push
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
    
    - name: Upload relay data as artifacts
      uses: actions/upload-artifact@v4
      with:
        name: relay-data-${{ github.run_number }}
        path: |
          relay_discovery_results.json
          nostr_relays.csv
        retention-days: 30
