name: Release

on:
  push:
    tags:
      - 'v*'

env:
  CARGO_TERM_COLOR: always
  BINARY_NAME: cxg

permissions:
  contents: write

jobs:
  build:
    name: Build ${{ matrix.target }}
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - os: ubuntu-latest
            target: x86_64-unknown-linux-gnu
            artifact: cxg-linux-amd64
          - os: ubuntu-latest
            target: aarch64-unknown-linux-gnu
            artifact: cxg-linux-arm64
            use_cross: true
          # Use macos-15-intel for x86_64 builds (the only free Intel runner available)
          - os: macos-15-intel
            target: x86_64-apple-darwin
            artifact: cxg-darwin-amd64
          # Use macos-latest (ARM64) for aarch64 builds
          - os: macos-latest
            target: aarch64-apple-darwin
            artifact: cxg-darwin-arm64
          - os: windows-latest
            target: x86_64-pc-windows-msvc
            artifact: cxg-windows-amd64
            ext: .exe

    steps:
      - uses: actions/checkout@v4

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

      - name: Install cross (for cross-compilation)
        if: matrix.use_cross
        run: |
          cargo install cross --git https://github.com/cross-rs/cross

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

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

      - name: Prepare artifact (Unix)
        if: runner.os != 'Windows'
        run: |
          mkdir -p dist
          cp target/${{ matrix.target }}/release/${{ env.BINARY_NAME }} dist/${{ matrix.artifact }}
          chmod +x dist/${{ matrix.artifact }}

      - name: Prepare artifact (Windows)
        if: runner.os == 'Windows'
        run: |
          mkdir dist
          copy target\${{ matrix.target }}\release\${{ env.BINARY_NAME }}.exe dist\${{ matrix.artifact }}.exe

      - name: Upload artifact
        uses: actions/upload-artifact@v4
        with:
          name: ${{ matrix.artifact }}
          path: dist/*

  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: Prepare release assets
        run: |
          mkdir -p release
          find artifacts -type f -exec mv {} release/ \;
          cd release
          sha256sum * > SHA256SUMS
          cat SHA256SUMS

      - name: Create GitHub Release
        uses: softprops/action-gh-release@v1
        with:
          files: release/*
          generate_release_notes: true
          draft: false
          prerelease: ${{ contains(github.ref, 'alpha') || contains(github.ref, 'beta') || contains(github.ref, 'rc') }}
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

  docker:
    name: Build Docker Image
    needs: build
    runs-on: ubuntu-latest
    permissions:
      contents: read
      packages: write
    steps:
      - uses: actions/checkout@v4

      - name: Download Linux AMD64 binary
        uses: actions/download-artifact@v4
        with:
          name: cxg-linux-amd64
          path: .

      - name: Download Linux ARM64 binary
        uses: actions/download-artifact@v4
        with:
          name: cxg-linux-arm64
          path: .

      - name: Set up QEMU (for multi-arch builds)
        uses: docker/setup-qemu-action@v3

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3

      - name: Login to GitHub Container Registry
        uses: docker/login-action@v3
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Extract version
        id: version
        run: |
          echo "VERSION=${GITHUB_REF#refs/tags/v}" >> $GITHUB_OUTPUT
          echo "REPO_LC=$(echo '${{ github.repository }}' | tr '[:upper:]' '[:lower:]')" >> $GITHUB_OUTPUT

      - name: Build and push multi-arch image
        uses: docker/build-push-action@v5
        with:
          context: .
          push: true
          tags: |
            ghcr.io/${{ steps.version.outputs.REPO_LC }}:${{ steps.version.outputs.VERSION }}
            ghcr.io/${{ steps.version.outputs.REPO_LC }}:latest
          platforms: linux/amd64,linux/arm64

  # Update Homebrew tap
  homebrew:
    name: Update Homebrew Tap
    needs: release
    runs-on: ubuntu-latest
    steps:
      - name: Extract version
        id: version
        run: echo "VERSION=${GITHUB_REF#refs/tags/v}" >> $GITHUB_OUTPUT

      - name: Trigger Homebrew tap update
        run: |
          gh workflow run update-formula.yml \
            -f version=${{ steps.version.outputs.VERSION }} \
            -R Bugb-Technologies/homebrew-cxg
        env:
          GITHUB_TOKEN: ${{ secrets.TAP_GITHUB_TOKEN }}

  # Publish to crates.io
  crates:
    name: Publish to crates.io
    needs: release
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

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

      - name: Publish to crates.io
        run: cargo publish --token ${{ secrets.CARGO_REGISTRY_TOKEN }}
        continue-on-error: true  # Don't fail if version already exists
