name: Build Multi-Platform Binaries

on:
  push:
    paths:
      - '**.go'
      - 'go.mod'
      - 'go.sum'
      - '.gitea/workflows/**'

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Setup Go
        uses: actions/setup-go@v5
        with:
          go-version: '1.24'
          cache: true

      - name: Build all platforms
        run: |
          mkdir -p dist

          # Linux amd64
          CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-s -w" \
            -o dist/server-linux-amd64 ./cmd/server
          CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-s -w" \
            -o dist/client-linux-amd64 ./cmd/client

          # Linux arm64
          CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -ldflags="-s -w" \
            -o dist/server-linux-arm64 ./cmd/server
          CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -ldflags="-s -w" \
            -o dist/client-linux-arm64 ./cmd/client

          # Darwin amd64
          CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -ldflags="-s -w" \
            -o dist/server-darwin-amd64 ./cmd/server
          CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -ldflags="-s -w" \
            -o dist/client-darwin-amd64 ./cmd/client

          # Darwin arm64
          CGO_ENABLED=0 GOOS=darwin GOARCH=arm64 go build -ldflags="-s -w" \
            -o dist/server-darwin-arm64 ./cmd/server
          CGO_ENABLED=0 GOOS=darwin GOARCH=arm64 go build -ldflags="-s -w" \
            -o dist/client-darwin-arm64 ./cmd/client

          # Windows amd64
          CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -ldflags="-s -w" \
            -o dist/server-windows-amd64.exe ./cmd/server
          CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -ldflags="-s -w" \
            -o dist/client-windows-amd64.exe ./cmd/client

      - name: List artifacts
        run: ls -lah dist/

      - name: Upload artifacts
        uses: actions/upload-artifact@v3
        with:
          name: gotunnel-binaries
          path: dist/
