name: Dev Release Test

on:
  workflow_dispatch:

env:
  CARGO_TERM_COLOR: always
  RUST_BACKTRACE: 1

jobs:
  test-build-binaries:
    name: Test Build Binaries
    strategy:
      fail-fast: false
      matrix:
        include:
          - target: x86_64-unknown-linux-gnu
            os: ubuntu-latest
            name: linux-x64
            artifact_name: pysentry
          - target: x86_64-unknown-linux-musl
            os: ubuntu-latest
            name: linux-x64-musl
            artifact_name: pysentry
          - target: aarch64-unknown-linux-gnu
            os: ubuntu-latest
            name: linux-arm64
            artifact_name: pysentry
          - target: x86_64-apple-darwin
            os: macos-latest
            name: macos-x64
            artifact_name: pysentry
          - target: aarch64-apple-darwin
            os: macos-latest
            name: macos-arm64
            artifact_name: pysentry
          - target: x86_64-pc-windows-msvc
            os: windows-latest
            name: windows-x64
            artifact_name: pysentry.exe

    runs-on: ${{ matrix.os }}
    steps:
      - uses: actions/checkout@v4

      - name: Install Rust
        uses: dtolnay/rust-toolchain@stable
        with:
          targets: ${{ matrix.target }}

      - name: Cache Rust dependencies
        uses: Swatinem/rust-cache@v2
        with:
          prefix-key: "release-${{ matrix.target }}"
          shared-key: "pysentry"

      - name: Install cross
        if: matrix.target != 'x86_64-unknown-linux-gnu'
        run: cargo install cross

      - name: Build binary (native)
        if: matrix.target == 'x86_64-unknown-linux-gnu'
        run: cargo build --release --target ${{ matrix.target }} --bin pysentry

      - name: Build binary (cross-compile)
        if: matrix.target != 'x86_64-unknown-linux-gnu'
        run: cross build --release --target ${{ matrix.target }} --bin pysentry

      - name: Package binary (Unix)
        if: matrix.os != 'windows-latest'
        run: |
          name=pysentry-${{ matrix.name }}
          mkdir $name
          cp target/${{ matrix.target }}/release/${{ matrix.artifact_name }} $name/
          cp README.md $name/
          cp LICENSE $name/
          tar -czf $name.tar.gz $name
          echo "ASSET=$name.tar.gz" >> $GITHUB_ENV

      - name: Package binary (Windows)
        if: matrix.os == 'windows-latest'
        run: |
          $name = "pysentry-${{ matrix.name }}"
          mkdir $name
          cp target/${{ matrix.target }}/release/${{ matrix.artifact_name }} $name/
          cp README.md $name/
          cp LICENSE $name/
          Compress-Archive -Path $name -DestinationPath "$name.zip"
          echo "ASSET=$name.zip" | Out-File -FilePath $env:GITHUB_ENV -Append

      - name: Upload binary artifact
        uses: actions/upload-artifact@v4
        with:
          name: binary-${{ matrix.name }}
          path: ${{ env.ASSET }}
          retention-days: 7

  test-build-python-wheels:
    name: Test Build Python Wheels
    runs-on: ${{ matrix.osarch.os }}
    strategy:
      fail-fast: false
      matrix:
        osarch:
          - os: ubuntu-latest
            target: x86_64-unknown-linux-gnu
          - os: ubuntu-latest
            target: aarch64-unknown-linux-gnu
          - os: windows-latest
            target: x86_64-pc-windows-msvc
          - os: macos-latest
            target: x86_64-apple-darwin
          - os: macos-latest
            target: aarch64-apple-darwin
        python:
          ["3.9", "3.10", "3.11", "3.12", "3.13", "3.13t", "3.14", "3.14t"]
    steps:
      - uses: actions/checkout@v4

      - name: Install system dependencies (Ubuntu)
        if: startsWith(matrix.osarch.os, 'ubuntu')
        run: sudo apt-get update && sudo apt-get install -y libssl-dev pkg-config

      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: ${{ matrix.python }}
          allow-prereleases: true

      - name: Install Rust
        uses: dtolnay/rust-toolchain@stable

      - name: Set manylinux version
        id: manylinux
        if: startsWith(matrix.osarch.os, 'ubuntu')
        run: |
          if [[ "${{ matrix.osarch.target }}" == "aarch64-unknown-linux-gnu" ]]; then
            echo "version=2_28" >> $GITHUB_OUTPUT
          else
            echo "version=auto" >> $GITHUB_OUTPUT
          fi

      - name: Build wheels
        uses: PyO3/maturin-action@v1.49.4
        with:
          command: build
          args: --release --features python --out dist -i ${{ matrix.python }}
          sccache: "true"
          manylinux: ${{ steps.manylinux.outputs.version || 'auto' }}
          target: ${{ matrix.osarch.target }}

      - name: Upload wheels
        uses: actions/upload-artifact@v4
        with:
          name: wheels-${{ matrix.osarch.os }}-${{ matrix.osarch.target }}-${{ matrix.python }}
          path: dist/*.whl
          retention-days: 7

  test-build-sdist:
    name: Test Build Source Distribution
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: "3.12"

      - name: Install Rust
        uses: dtolnay/rust-toolchain@stable

      - name: Build sdist
        uses: PyO3/maturin-action@v1.49.4
        with:
          command: sdist
          args: --out dist

      - name: Upload sdist
        uses: actions/upload-artifact@v4
        with:
          name: sdist
          path: dist/*.tar.gz
          retention-days: 7

  test-release-summary:
    name: Release Test Summary
    runs-on: ubuntu-latest
    needs: [test-build-binaries, test-build-python-wheels, test-build-sdist]
    if: always()
    steps:
      - name: Check build results
        run: |
          echo "## Build Results" >> $GITHUB_STEP_SUMMARY
          echo "" >> $GITHUB_STEP_SUMMARY

          if [ "${{ needs.test-build-binaries.result }}" == "success" ]; then
            echo "✅ Binary builds: SUCCESS" >> $GITHUB_STEP_SUMMARY
          else
            echo "❌ Binary builds: FAILED" >> $GITHUB_STEP_SUMMARY
          fi

          if [ "${{ needs.test-build-python-wheels.result }}" == "success" ]; then
            echo "✅ Python wheel builds: SUCCESS" >> $GITHUB_STEP_SUMMARY
          else
            echo "❌ Python wheel builds: FAILED" >> $GITHUB_STEP_SUMMARY
          fi

          if [ "${{ needs.test-build-sdist.result }}" == "success" ]; then
            echo "✅ Source distribution build: SUCCESS" >> $GITHUB_STEP_SUMMARY
          else
            echo "❌ Source distribution build: FAILED" >> $GITHUB_STEP_SUMMARY
          fi

          echo "" >> $GITHUB_STEP_SUMMARY
          echo "All artifacts are available in the workflow run for 7 days." >> $GITHUB_STEP_SUMMARY

      - name: Fail if any build failed
        if: needs.test-build-binaries.result != 'success' || needs.test-build-python-wheels.result != 'success' || needs.test-build-sdist.result != 'success'
        run: exit 1
