name: TinyGo Build

on:
  push:
    branches: [ "tinygo" ]
  pull_request:
    branches: [ "tinygo" ]
  workflow_dispatch:

jobs:
  tinygo-build-all:
    permissions:
      contents: read

    runs-on: ubuntu-latest

    steps:
    - name: Checkout code
      uses: https://git.quad4.io/actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0

    - name: Set up Go
      uses: https://git.quad4.io/actions/setup-go@44694675825211faa026b3c33043df3e48a5fa00
      with:
        go-version: '1.24'

    - name: Setup Task
      uses: https://git.quad4.io/actions/setup-task@0ab1b2a65bc55236a3bc64cde78f80e20e8885c2 # v1
      with:
        version: '3.46.3'

    - name: Install TinyGo
      run: |
        wget https://github.com/tinygo-org/tinygo/releases/download/v0.37.0/tinygo_0.37.0_amd64.deb
        sudo dpkg -i tinygo_0.37.0_amd64.deb

    - name: Build for all TinyGo targets
      id: build_step
      run: |
        task tinygo-build-all || true
        echo "Build process completed (some targets may have failed)"

    - name: Collect build results
      run: |
        mkdir -p artifacts
        unsupported_file="artifacts/unsupported-microcontrollers.txt"
        echo "# Unsupported Microcontrollers" > "$unsupported_file"
        echo "# Generated: $(date -u +"%Y-%m-%d %H:%M:%S UTC")" >> "$unsupported_file"
        echo "" >> "$unsupported_file"
        
        failed_count=0
        for log_file in bin/build-*.log; do
          if [ -f "$log_file" ]; then
            target=$(basename "$log_file" | sed 's/build-\(.*\)\.log/\1/')
            binary_file="bin/reticulum-go-${target}"
            
            if [ ! -f "$binary_file" ] || grep -qi "error\|Error\|ERROR\|failed\|Failed\|FAILED" "$log_file"; then
              failed_count=$((failed_count + 1))
              echo "## $target" >> "$unsupported_file"
              echo "" >> "$unsupported_file"
              
              if grep -qi "program too large\|overflowed\|too big\|LLVM ERROR\|Error while" "$log_file"; then
                grep -i "program too large\|overflowed\|too big\|LLVM ERROR\|Error while" "$log_file" | head -5 >> "$unsupported_file"
              else
                tail -15 "$log_file" >> "$unsupported_file"
              fi
              
              echo "" >> "$unsupported_file"
              echo "\`\`\`" >> "$unsupported_file"
              tail -30 "$log_file" >> "$unsupported_file"
              echo "\`\`\`" >> "$unsupported_file"
              echo "" >> "$unsupported_file"
            fi
          fi
        done
        
        echo "Total failed builds: $failed_count" >> "$unsupported_file"
        echo "Generated unsupported-microcontrollers.txt with $failed_count failed targets"

    - name: Upload build artifacts
      uses: https://git.quad4.io/actions/upload-artifact@ff15f0306b3f739f7b6fd43fb5d26cd321bd4de5
      with:
        name: tinygo-builds
        path: |
          bin/reticulum-go-*
          artifacts/unsupported-microcontrollers.txt
        if-no-files-found: warn
