name: Release

on:
  push:
    branches:
      - release

permissions:
  contents: write

jobs:
  build:
    name: Build ${{ matrix.target }}
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        include:
          - os: ubuntu-latest
            target: x86_64-unknown-linux-gnu
            artifact_name: base-d
            asset_name: base-d-linux-x86_64
          - os: ubuntu-latest
            target: aarch64-unknown-linux-gnu
            artifact_name: base-d
            asset_name: base-d-linux-aarch64
          - os: ubuntu-latest
            target: x86_64-unknown-linux-musl
            artifact_name: base-d
            asset_name: base-d-linux-x86_64-musl
          - os: ubuntu-latest
            target: aarch64-unknown-linux-musl
            artifact_name: base-d
            asset_name: base-d-linux-aarch64-musl
          - os: ubuntu-latest
            target: x86_64-unknown-freebsd
            artifact_name: base-d
            asset_name: base-d-freebsd-x86_64
          - os: windows-latest
            target: x86_64-pc-windows-msvc
            artifact_name: base-d.exe
            asset_name: base-d-windows-x86_64.exe
          - os: windows-latest
            target: aarch64-pc-windows-msvc
            artifact_name: base-d.exe
            asset_name: base-d-windows-aarch64.exe
          - os: macos-latest
            target: x86_64-apple-darwin
            artifact_name: base-d
            asset_name: base-d-macos-x86_64
          - os: macos-latest
            target: aarch64-apple-darwin
            artifact_name: base-d
            asset_name: base-d-macos-aarch64

    steps:
      - uses: actions/checkout@v4

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

      - name: Install cross (cross-compilation targets)
        if: matrix.target == 'aarch64-unknown-linux-gnu' || matrix.target == 'x86_64-unknown-freebsd' || contains(matrix.target, 'musl')
        run: cargo install cross --git https://github.com/cross-rs/cross

      - name: Build x86 Linux
        if: matrix.target == 'x86_64-unknown-linux-gnu'
        run: cargo build --release --target ${{ matrix.target }}

      - name: Build ARM Linux
        if: matrix.target == 'aarch64-unknown-linux-gnu'
        run: cross build --release --target ${{ matrix.target }}

      - name: Build x86 MUSL Linux
        if: matrix.target == 'x86_64-unknown-linux-musl'
        run: cross build --release --target ${{ matrix.target }}

      - name: Build ARM MUSL Linux
        if: matrix.target == 'aarch64-unknown-linux-musl'
        run: cross build --release --target ${{ matrix.target }}

      - name: Build x86 FreeBSD
        if: matrix.target == 'x86_64-unknown-freebsd'
        run: cross build --release --target ${{ matrix.target }}

      - name: Build x86 Windows
        if: matrix.target == 'x86_64-pc-windows-msvc'
        run: cargo build --release --target ${{ matrix.target }}

      - name: Build ARM Windows
        if: matrix.target == 'aarch64-pc-windows-msvc'
        run: cargo build --release --target ${{ matrix.target }}

      - name: Build x86 macOS
        if: matrix.target == 'x86_64-apple-darwin'
        run: cargo build --release --target ${{ matrix.target }}

      - name: Build ARM macOS
        if: matrix.target == 'aarch64-apple-darwin'
        run: cargo build --release --target ${{ matrix.target }}

      - name: Upload artifact
        uses: actions/upload-artifact@v4
        with:
          name: ${{ matrix.asset_name }}
          path: target/${{ matrix.target }}/release/${{ matrix.artifact_name }}

      - name: Package binary (Linux/FreeBSD)
        if: contains(matrix.target, 'linux') || contains(matrix.target, 'freebsd')
        run: |
          cd target/${{ matrix.target }}/release
          tar -czf ${{ matrix.asset_name }}.tar.gz ${{ matrix.artifact_name }}
          mv ${{ matrix.asset_name }}.tar.gz ../../..

      - name: Package binary (Windows)
        if: contains(matrix.target, 'windows')
        shell: pwsh
        run: |
          Compress-Archive -Path target/${{ matrix.target }}/release/${{ matrix.artifact_name }} -DestinationPath ${{ matrix.asset_name }}.zip

      - name: Package binary (macOS)
        if: contains(matrix.target, 'apple')
        run: |
          cd target/${{ matrix.target }}/release
          tar -czf ${{ matrix.asset_name }}.tar.gz ${{ matrix.artifact_name }}
          mv ${{ matrix.asset_name }}.tar.gz ../../..

      - name: Upload packaged artifact (Linux/FreeBSD)
        if: contains(matrix.target, 'linux') || contains(matrix.target, 'freebsd')
        uses: actions/upload-artifact@v4
        with:
          name: ${{ matrix.asset_name }}-packaged
          path: ${{ matrix.asset_name }}.tar.gz

      - name: Upload packaged artifact (Windows)
        if: contains(matrix.target, 'windows')
        uses: actions/upload-artifact@v4
        with:
          name: ${{ matrix.asset_name }}-packaged
          path: ${{ matrix.asset_name }}.zip

      - name: Upload packaged artifact (macOS)
        if: contains(matrix.target, 'apple')
        uses: actions/upload-artifact@v4
        with:
          name: ${{ matrix.asset_name }}-packaged
          path: ${{ matrix.asset_name }}.tar.gz

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

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

      - name: Create Release
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          # Get version from Cargo.toml
          VERSION="v$(grep '^version' Cargo.toml | head -1 | cut -d'"' -f2)"
          
          # Check if release already exists
          if gh release view "$VERSION" >/dev/null 2>&1; then
            echo "Release $VERSION already exists, skipping"
            exit 0
          fi
          
          # Delete tag if it exists without a release
          git push origin :refs/tags/"$VERSION" 2>/dev/null || true
          
          # Create new tag
          git tag "$VERSION"
          git push origin "$VERSION"
          
          # Create release
          gh release create "$VERSION" \
            --title "Release $VERSION" \
            --generate-notes
          
          # Upload all packaged binaries
          for dir in artifacts/*-packaged/; do
            if [ -d "$dir" ]; then
              for file in "$dir"*.tar.gz "$dir"*.zip; do
                if [ -f "$file" ]; then
                  gh release upload "$VERSION" "$file"
                fi
              done
            fi
          done
