name: Build and Release Packages and Binaries

on:
  workflow_dispatch:
  push:
    tags:
      - "v*"

permissions:
  contents: write

jobs:
  build-openwrt:
    name: Build ${{ matrix.arch }} on ${{ matrix.version }}
    runs-on: ubuntu-latest
    strategy:
      fail-fast: true
      matrix:
        arch:
          - x86_64
          - aarch64_generic
          - aarch64_cortex-a53
          - aarch64_cortex-a72
          - aarch64_cortex-a76
          - arm_cortex-a7_neon-vfpv4
          - arm_cortex-a9_vfpv3-d16
          - arm_cortex-a15_neon-vfpv4
          - mips_24kc
          - mipsel_24kc
        version:
          - 24.10.6
          - 25.12.2

    steps:
      - uses: actions/checkout@v5

      - name: Install dependencies
        run: sudo apt-get update && sudo apt-get install -y llvm clang linux-headers-$(uname -r) libbpf-dev gcc-multilib

      - name: Set up Go
        uses: actions/setup-go@v6
        with:
          go-version: "1.23"
          cache-dependency-path: go.sum

      - name: Generate BPF code
        run: go generate ./internal/bpf/...

      - name: Build
        uses: openwrt/gh-action-sdk@v9
        env:
          ARCH: ${{ matrix.arch }}-${{ matrix.version }}
          FEEDNAME: ua3f_ci
          PACKAGES: openwrt

      - name: Rename packages with arch suffix
        run: |
          cd bin/packages/${{ matrix.arch }}/ua3f_ci/
          for f in *.apk; do mv "$f" "${f/.apk/-${{ matrix.arch }}.apk}" 2>/dev/null || true; done

      - name: Upload packages
        uses: actions/upload-artifact@v4
        with:
          name: packages-${{ matrix.arch }}-${{ matrix.version }}
          path: bin/packages/${{ matrix.arch }}/ua3f_ci/*.*pk
          retention-days: 1

  build-binaries:
    name: Build ${{ matrix.goos }}/${{ matrix.goarch }}
    runs-on: ubuntu-latest
    strategy:
      fail-fast: true
      matrix:
        include:
          - goos: linux
            goarch: amd64
          - goos: linux
            goarch: arm64
          - goos: windows
            goarch: amd64
          - goos: windows
            goarch: arm64
          - goos: darwin
            goarch: arm64
          - goos: android
            goarch: arm64

    steps:
      - uses: actions/checkout@v5

      - name: Set up Go
        uses: actions/setup-go@v6
        with:
          go-version: "1.23"
          cache-dependency-path: go.sum

      - name: Install dependencies
        run: sudo apt-get update && sudo apt-get install -y zip llvm clang linux-headers-$(uname -r) libbpf-dev gcc-multilib

      - name: Get version
        id: get_version
        run: |
          if [[ "$GITHUB_REF_TYPE" == "tag" ]]; then
            echo "VERSION=${GITHUB_REF_NAME#v}" >> $GITHUB_OUTPUT
          else
            echo "VERSION=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT
          fi

      - name: Build binary
        run: |
          VERSION="${{ steps.get_version.outputs.VERSION }}"
          GOOS="${{ matrix.goos }}"
          GOARCH="${{ matrix.goarch }}"

          echo "Building for $GOOS/$GOARCH..."

          go generate ./...

          CGO_ENABLED=0 GOOS=$GOOS GOARCH=$GOARCH \
            go build -trimpath -ldflags="-s -w" -o ua3f main.go

          mkdir -p dist

          case "$GOOS" in
            windows)
              mv ua3f ua3f.exe
              zip "dist/ua3f-${VERSION}-${GOOS}-${GOARCH}.zip" -j ua3f.exe
              ;;
            darwin)
              zip -q "dist/ua3f-${VERSION}-${GOOS}-${GOARCH}.zip" -j ua3f
              ;;
            *)
              tar -czf "dist/ua3f-${VERSION}-${GOOS}-${GOARCH}.tar.gz" ua3f
              ;;
          esac

      - name: Upload binary artifacts
        uses: actions/upload-artifact@v4
        with:
          name: binary-${{ matrix.goos }}-${{ matrix.goarch }}
          path: dist/*
          retention-days: 1

  release:
    needs: [build-openwrt, build-binaries]
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v5
        if: github.ref_type != 'tag'

      - name: Download OpenWrt packages
        uses: actions/download-artifact@v4
        with:
          pattern: packages-*
          path: dist/
          merge-multiple: true

      - name: Download binary packages
        uses: actions/download-artifact@v4
        with:
          pattern: binary-*
          path: dist/
          merge-multiple: true

      - name: Get version
        id: get_version
        run: |
          if [[ "$GITHUB_REF_TYPE" == "tag" ]]; then
            echo "VERSION=${GITHUB_REF_NAME}" >> $GITHUB_OUTPUT
          else
            echo "VERSION=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT
          fi

      - name: Release
        uses: softprops/action-gh-release@v2
        if: github.ref_type == 'tag'
        with:
          name: UA3F ${{ steps.get_version.outputs.VERSION }}
          files: dist/*
          draft: true
          prerelease: false
          generate_release_notes: true
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Release Nightly
        uses: softprops/action-gh-release@v2
        if: github.ref_type != 'tag'
        with:
          name: UA3F ${{ steps.get_version.outputs.VERSION }}
          files: dist/*
          draft: true
          prerelease: true
          tag_name: nightly-${{ steps.get_version.outputs.VERSION }}
          generate_release_notes: false
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
