name: CD - create release

on:
  push:
    branches:
      - main
    paths:
      - 'Cargo.toml'
  workflow_dispatch:

jobs:
  check-version:
    runs-on: ubuntu-latest
    outputs:
      version_changed: ${{ steps.version-check.outputs.version-changed }}
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Check if version changed
        id: version-check
        uses: joaommartins/cargo-version-check-action@v1
        with:
          base-ref: ${{ github.base_ref }}

  build-artifacts:
    needs:
      - check-version
    if: needs.check-version.outputs.version_changed == 'true' || github.event_name == 'workflow_dispatch'
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Set up Rust toolchain
        uses: actions-rs/toolchain@v1
        with:
          toolchain: stable
          profile: minimal
          override: true

      - name: Cache cargo registry and build
        uses: actions/cache@v4
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
            target
          key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock', '**/Cargo.toml') }}
          restore-keys: |
            ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.toml') }}

      - name: Install cargo-aur
        uses: taiki-e/install-action@v2
        with:
          tool: cargo-aur

      - name: Build release binary
        run: |
          cargo build --release --locked

      - name: Upload release binary
        uses: actions/upload-artifact@v4
        with:
          name: arbol-binary
          path: target/release/arbol

  create-release:
    needs:
      - check-version
      - build-artifacts
    if: needs.check-version.outputs.version_changed == 'true' || github.event_name == 'workflow_dispatch'
    runs-on: ubuntu-latest
    permissions:
      contents: write

    steps:
      - name: Checkout code
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Download release binary
        uses: actions/download-artifact@v4
        with:
          name: arbol-binary
          path: target/release

      - name: Create Release and Upload Assets
        uses: softprops/action-gh-release@v2
        with:
          tag_name: v${{ needs.check-version.outputs.current_version }}
          name: v${{ needs.check-version.outputs.current_version }}
          draft: false
          prerelease: false
          fail_on_unmatched_files: true
          generate_release_notes: true
          files: |
            target/release/arbol
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

  push-to-crates-io:
    needs:
      - create-release
    if: needs.check-version.outputs.version_changed == 'true' || github.event_name == 'workflow_dispatch'
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

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

      - name: Publish to crates.io
        run: cargo publish --token ${{ secrets.CARGO_REGISTRY_TOKEN }} || true
