name: Release

on:
  push:
    tags:
      - 'v*'

permissions:
  contents: write

jobs:
  build:
    name: Build ${{ matrix.target }}
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false  # Don't cancel other jobs if one fails
      matrix:
        include:
          # macOS builds
          - os: macos-latest
            target: x86_64-apple-darwin
            binary_name: fastcert
            asset_name: fastcert-x86_64-apple-darwin
          - os: macos-latest
            target: aarch64-apple-darwin
            binary_name: fastcert
            asset_name: fastcert-aarch64-apple-darwin

          # Linux x86_64
          - os: ubuntu-latest
            target: x86_64-unknown-linux-gnu
            binary_name: fastcert
            asset_name: fastcert-x86_64-unknown-linux-gnu

          # Linux ARM64 (using cross)
          - os: ubuntu-latest
            target: aarch64-unknown-linux-gnu
            binary_name: fastcert
            asset_name: fastcert-aarch64-unknown-linux-gnu
            use_cross: true

          # Windows
          - os: windows-latest
            target: x86_64-pc-windows-msvc
            binary_name: fastcert.exe
            asset_name: fastcert-x86_64-pc-windows-msvc.exe

    steps:
      - name: Checkout code
        uses: actions/checkout@v4

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

      - name: Install cross
        if: matrix.use_cross
        run: cargo install cross --git https://github.com/cross-rs/cross

      - name: Build with cross
        if: matrix.use_cross
        run: cross build --release --target ${{ matrix.target }}

      - name: Build with cargo
        if: ${{ !matrix.use_cross }}
        run: cargo build --release --target ${{ matrix.target }}

      - name: Strip binary (Unix)
        if: matrix.os != 'windows-latest' && !matrix.use_cross
        run: strip target/${{ matrix.target }}/release/${{ matrix.binary_name }}

      - name: Create archive (Unix)
        if: matrix.os != 'windows-latest'
        run: |
          cd target/${{ matrix.target }}/release
          tar czf ../../../${{ matrix.asset_name }}.tar.gz ${{ matrix.binary_name }}
          cd ../../..

      - name: Create archive (Windows)
        if: matrix.os == 'windows-latest'
        run: |
          cd target/${{ matrix.target }}/release
          7z a ../../../${{ matrix.asset_name }}.zip ${{ matrix.binary_name }}
          cd ../../..

      - name: Upload artifact (Unix)
        if: matrix.os != 'windows-latest'
        uses: actions/upload-artifact@v4
        with:
          name: ${{ matrix.asset_name }}
          path: ${{ matrix.asset_name }}.tar.gz

      - name: Upload artifact (Windows)
        if: matrix.os == 'windows-latest'
        uses: actions/upload-artifact@v4
        with:
          name: ${{ matrix.asset_name }}
          path: ${{ matrix.asset_name }}.zip

  release:
    name: Create Release
    needs: build
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Download all artifacts
        uses: actions/download-artifact@v4
        with:
          path: artifacts

      - name: Display structure of downloaded files
        run: ls -R artifacts

      - name: Create Release
        uses: softprops/action-gh-release@v1
        with:
          files: artifacts/**/*
          draft: false
          prerelease: false
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
