name: Go Build Multi-Platform

on:
  push:
    branches: [ "main", "master" ]
    tags:
      - 'v*'
  pull_request:
    branches: [ "main", "master" ]

jobs:
  build:
    permissions:
      contents: write
    strategy:
      matrix:
        goos: [linux, windows, darwin, freebsd]
        goarch: [amd64, arm64, arm]
        include:
          - goos: js
            goarch: wasm
        exclude:
          - goos: darwin
            goarch: arm

    runs-on: ubuntu-latest

    outputs:
      build_complete: ${{ steps.build_step.outcome == 'success' }}

    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 # v6.0.0
      with:
        go-version: '1.25'

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

    - name: Build
      id: build_step
      env:
        GOOS: ${{ matrix.goos }}
        GOARCH: ${{ matrix.goarch }}
        GOARM: ${{ matrix.goarch == 'arm' && '6' || '' }}
        CGO_ENABLED: '0'
      run: |
        output_name="reticulum-go-${GOOS}-${GOARCH}"
        if [ "$GOOS" = "js" ] && [ "$GOARCH" = "wasm" ]; then
          task build-wasm
          output_name+=".wasm"
          mv bin/reticulum-go.wasm "${output_name}"
        else
          task build
          if [ "$GOOS" = "windows" ]; then
            output_name+=".exe"
          fi
          mv bin/reticulum-go "${output_name}"
        fi
        echo "Built: ${output_name}"

    - name: Calculate SHA256 Checksum
      run: |
        output_name="reticulum-go-${{ matrix.goos }}-${{ matrix.goarch }}"
        if [ "${{ matrix.goos }}" = "windows" ]; then
          output_name+=".exe"
        elif [ "${{ matrix.goos }}" = "js" ] && [ "${{ matrix.goarch }}" = "wasm" ]; then
          output_name+=".wasm"
        fi
        BINARY_PATH="${output_name}" task checksum

    - name: Upload Artifact
      uses: https://git.quad4.io/actions/upload-artifact@ff15f0306b3f739f7b6fd43fb5d26cd321bd4de5 # v3.2.1
      with:
        name: reticulum-go-${{ matrix.goos }}-${{ matrix.goarch }}
        path: |
          reticulum-go-${{ matrix.goos }}-${{ matrix.goarch }}*

  release:
    name: Create Release
    runs-on: ubuntu-latest
    needs: build
    if: startsWith(github.ref, 'refs/tags/')

    permissions:
      contents: write

    steps:
      - name: Download All Build Artifacts
        uses: https://git.quad4.io/actions/download-artifact@9bc31d5ccc31df68ecc42ccf4149144866c47d8a
        with:
          path: ./release-assets

      - name: List downloaded files (for debugging)
        run: ls -R ./release-assets

      - name: Create Gitea Release
        uses: https://git.quad4.io/actions/gitea-release-action@4875285c0950474efb7ca2df55233c51333eeb74
        with:
          files: ./release-assets/*/*
