name: 🚀 Release Builds and Publish

on:
  push:
    tags:
      - "v*"
  workflow_dispatch:

jobs:
  build:
    name: 💠 Build for ${{ matrix.target }}
    runs-on: ${{ matrix.target == 'x86_64-apple-darwin' && 'macos-latest' || matrix.target == 'aarch64-apple-darwin' && 'macos-latest' || matrix.target == 'x86_64-pc-windows-gnu' && 'windows-latest' || matrix.target == 'x86_64-pc-windows-msvc' && 'windows-latest' || 'ubuntu-latest' }}
    strategy:
      matrix:
        include:
          # Linux
          - target: x86_64-unknown-linux-gnu
            os: ubuntu-latest
          # macOS
          - target: x86_64-apple-darwin
            os: macos-latest
          - target: aarch64-apple-darwin
            os: macos-latest
          # Windows
          - target: x86_64-pc-windows-gnu
            os: windows-latest
          - target: x86_64-pc-windows-msvc
            os: windows-latest
          # Android
          - target: aarch64-linux-android
            os: ubuntu-latest

    steps:
      - name: 🔍 Checkout code
        uses: actions/checkout@v3

      - name: ⚙️ Set up Rust
        uses: dtolnay/rust-toolchain@stable
        with:
          target: ${{ matrix.target }}
          components: rustfmt, clippy

      - name: 🔧 Setup Android build environment
        if: matrix.target == 'aarch64-linux-android'
        run: |
          wget https://dl.google.com/android/repository/android-ndk-r25c-linux.zip
          unzip android-ndk-r25c-linux.zip
          echo "ANDROID_NDK_HOME=$PWD/android-ndk-r25c" >> $GITHUB_ENV
          rustup target add aarch64-linux-android
          echo "CC_aarch64_linux_android=$PWD/android-ndk-r25c/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android21-clang" >> $GITHUB_ENV
          echo "CARGO_TARGET_AARCH64_LINUX_ANDROID_LINKER=$PWD/android-ndk-r25c/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android21-clang" >> $GITHUB_ENV

      - name: ⚖️ Build for Android
        if: matrix.target == 'aarch64-linux-android'
        run: |
          cargo build --target aarch64-linux-android --release
        env:
          CC: ${{ env.CC_aarch64_linux_android }}
          AR: ${{ env.ANDROID_NDK_HOME }}/toolchains/llvm/prebuilt/linux-x86_64/bin/llvm-ar

      - name: ⚖️ Build for Windows
        if: contains(matrix.target, 'windows')
        shell: pwsh
        run: |
          cargo build --target ${{ matrix.target }} --release

      - name: ⚖️ Build for Unix
        if: "!contains(matrix.target, 'windows') && matrix.target != 'aarch64-linux-android'"
        run: |
          cargo build --target ${{ matrix.target }} --release

      - name: 📁 Package binary (Windows)
        if: contains(matrix.target, 'windows')
        shell: pwsh
        run: |
          New-Item -ItemType Directory -Force -Path release
          Copy-Item "target/${{ matrix.target }}/release/getquotes.exe" -Destination "release/getquotes-${{ matrix.target }}.exe"

      - name: 📁 Package binary (Unix)
        if: "!contains(matrix.target, 'windows')"
        shell: bash
        run: |
          mkdir -p release
          cp "target/${{ matrix.target }}/release/getquotes" "release/getquotes-${{ matrix.target }}"

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

  release:
    name: 🚀 Create GitHub Release
    runs-on: ubuntu-latest
    needs: build
    if: startsWith(github.ref, 'refs/tags/')
    steps:
      - name: 📥 Download artifacts
        uses: actions/download-artifact@v4
        with:
          path: artifacts

      - name: 📝 Get version from tag
        id: get_version
        run: echo "VERSION=${GITHUB_REF#refs/tags/}" >> $GITHUB_OUTPUT

      - name: 🗿 Create GitHub Release
        uses: softprops/action-gh-release@v1
        with:
          tag_name: ${{ steps.get_version.outputs.VERSION }}
          name: Release ${{ steps.get_version.outputs.VERSION }}
          draft: false
          prerelease: false
          files: |
            artifacts/getquotes-x86_64-unknown-linux-gnu/getquotes-x86_64-unknown-linux-gnu
            artifacts/getquotes-x86_64-apple-darwin/getquotes-x86_64-apple-darwin
            artifacts/getquotes-aarch64-apple-darwin/getquotes-aarch64-apple-darwin
            artifacts/getquotes-x86_64-pc-windows-gnu/getquotes-x86_64-pc-windows-gnu.exe
            artifacts/getquotes-x86_64-pc-windows-msvc/getquotes-x86_64-pc-windows-msvc.exe
            artifacts/getquotes-aarch64-linux-android/getquotes-aarch64-linux-android
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

  publish:
    name: 📦 Publish to crates.io
    runs-on: ubuntu-latest
    needs: build
    if: startsWith(github.ref, 'refs/tags/')
    steps:
      - name: 🔍 Checkout code
        uses: actions/checkout@v3

      - name: ⚙️ Set up Rust
        uses: dtolnay/rust-toolchain@stable
        with:
          components: rustfmt, clippy

      - name: 🔑 Authenticate with crates.io
        run: echo "${{ secrets.CRATES_IO_TOKEN }}" | cargo login

      - name: 🚀 Publish to crates.io
        run: cargo publish
