name: Release Please

on:
  workflow_run:
    workflows: ["Rust CI"] # Name of the CI workflow file (rust.yml)
    types:
      - completed
    branches:
      - master

  # Keep workflow_call if needed for other workflows to get release info
  workflow_call:
    outputs:
      release_created:
        description: "Whether a release was created by release-please"
        value: ${{ jobs.release-please.outputs.created }}

permissions:
  contents: write
  pull-requests: write
  issues: write

jobs:
  release-please:
    name: Run release-please
    runs-on: ubuntu-latest
    # Add condition to only run if the triggering workflow succeeded
    if: ${{ github.event.workflow_run.conclusion == 'success' }}
    outputs:
      created: ${{ steps.release.outputs.release_created }}
      tag_name: ${{ steps.release.outputs.tag_name }}
      upload_url: ${{ steps.release.outputs.upload_url }}
    steps:
      # Checkout the code from the commit that triggered the Rust CI workflow
      - uses: actions/checkout@v4
        with:
          # Use the SHA from the workflow_run event
          ref: ${{ github.event.workflow_run.head_sha }}

      - uses: googleapis/release-please-action@v4
        id: release
        with:
          release-type: rust
          # Optionally configure package name if Cargo.toml is not at the root
          # package-name: your-crate-name
          # You might need a PAT instead of GITHUB_TOKEN if you hit permission issues
          # token: ${{ secrets.YOUR_PAT }}

  # build-and-upload-binaries:
  #   name: Build & Upload Binaries
  #   needs: release-please
  #   if: needs.release-please.outputs.created == 'true'
  #   runs-on: ${{ matrix.os }}
  #   permissions:
  #     contents: write # Needed to upload release assets
  #   strategy:
  #     matrix:
  #       include:
  #         - os: ubuntu-latest
  #           target: x86_64-unknown-linux-gnu
  #           asset_ext: tar.gz
  #           binary_ext: ""
  #         - os: macos-latest # Should default to x86_64 runner
  #           target: x86_64-apple-darwin
  #           asset_ext: tar.gz
  #           binary_ext: ""
  #         - os: macos-latest # M1/M2 Macs
  #           target: aarch64-apple-darwin
  #           asset_ext: tar.gz
  #           binary_ext: ""
  #         - os: windows-latest
  #           target: x86_64-pc-windows-msvc
  #           asset_ext: zip
  #           binary_ext: .exe

  #   steps:
  #     - name: Checkout code for the release tag
  #       uses: actions/checkout@v4
  #       with:
  #         ref: ${{ needs.release-please.outputs.tag_name }}

  #     - name: Set up Rust toolchain
  #       uses: dtolnay/rust-toolchain@stable
  #       with:
  #         target: ${{ matrix.target }}

  #     - name: Cache dependencies
  #       uses: Swatinem/rust-cache@v2

  #     - name: Build binary
  #       run: cargo build --release --target ${{ matrix.target }}

  #     - name: Prepare artifacts (Linux/macOS)
  #       if: matrix.os != 'windows-latest'
  #       run: |
  #         cd target/${{ matrix.target }}/release
  #         strip papersmith || true # Try stripping, ignore if fails
  #         tar czf ../../../papersmith-${{ needs.release-please.outputs.tag_name }}-${{ matrix.target }}.tar.gz papersmith
  #         cd ../../..
  #       shell: bash

  #     - name: Prepare artifacts (Windows)
  #       if: matrix.os == 'windows-latest'
  #       run: |
  #         cd target/${{ matrix.target }}/release
  #         # Use Compress-Archive from PowerShell standard library
  #         Compress-Archive -Path papersmith.exe -DestinationPath ../../../papersmith-${{ needs.release-please.outputs.tag_name }}-${{ matrix.target }}.zip
  #         cd ../../..
  #       shell: pwsh # Use PowerShell

  #     - name: Upload Release Asset
  #       uses: actions/upload-release-asset@v1
  #       env:
  #         GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  #       with:
  #         upload_url: ${{ needs.release-please.outputs.upload_url }}
  #         asset_path: ./papersmith-${{ needs.release-please.outputs.tag_name }}-${{ matrix.target }}.${{ matrix.asset_ext }}
  #         asset_name: papersmith-${{ needs.release-please.outputs.tag_name }}-${{ matrix.target }}.${{ matrix.asset_ext }}
  #         asset_content_type: application/zip # Use application/zip consistently

  # publish:
  #   name: Publish to crates.io
  #   needs: [release-please]
  #   runs-on: ubuntu-latest
  #   # This job also already checks if release-please created a release.
  #   if: needs.release-please.outputs.created == 'true'
  #   environment: # Optional: Use GitHub Environments for added security/control
  #     name: crates_io
  #     url: https://crates.io/crates/papersmith # Replace 'papersmith' if your crate name differs
  #   steps:
  #     - uses: actions/checkout@v4
  #       with:
  #         # Checkout the specific tag created by release-please
  #         ref: ${{ needs.release-please.outputs.tag_name }}

  #     - name: Set up Rust toolchain
  #       uses: dtolnay/rust-toolchain@stable

  #     - name: Cache cargo dependencies
  #       uses: Swatinem/rust-cache@v2

  #     - name: Publish crate
  #       run: cargo publish --token ${{ secrets.CARGO_REGISTRY_TOKEN }}
  #       env:
  #         CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
