name: Release

on:
  push:
    tags:
      - "v*"

permissions:
  contents: write

env:
  CARGO_TERM_COLOR: always
  CARGO_INCREMENTAL: 0

jobs:
  build:
    name: Build (${{ matrix.target }})
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        include:
          # Linux x86_64 — static musl (cross handles OpenSSL for musl)
          - target: x86_64-unknown-linux-musl
            os: ubuntu-latest
            cross: true
            ext: ""
            archive: tar.gz

          # Linux ARM64 — for Raspberry Pi, ARM VPS
          - target: aarch64-unknown-linux-musl
            os: ubuntu-latest
            cross: true
            ext: ""
            archive: tar.gz

          # macOS Intel (cross-compiled from Apple Silicon runner)
          - target: x86_64-apple-darwin
            os: macos-latest
            cross: false
            ext: ""
            archive: tar.gz

          # macOS Apple Silicon
          - target: aarch64-apple-darwin
            os: macos-latest
            cross: false
            ext: ""
            archive: tar.gz

          # Windows x86_64
          - target: x86_64-pc-windows-msvc
            os: windows-latest
            cross: false
            ext: ".exe"
            archive: zip

    steps:
      - uses: actions/checkout@v6

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

      - name: Cache cargo
        uses: actions/cache@v5
        with:
          path: |
            ~/.cargo/registry/index
            ~/.cargo/registry/cache
            ~/.cargo/git/db
            target
          key: release-${{ matrix.target }}-${{ hashFiles('**/Cargo.lock') }}
          restore-keys: release-${{ matrix.target }}-

      # cross handles musl OpenSSL automatically via Docker image
      - name: Install cross
        if: matrix.cross
        run: cargo install cross --locked

      - name: Build (native)
        if: "!matrix.cross"
        run: cargo build --release --bin garudust --bin garudust-server --target ${{ matrix.target }}

      - name: Build (cross)
        if: matrix.cross
        run: cross build --release --bin garudust --bin garudust-server --target ${{ matrix.target }}

      # ── Package ────────────────────────────────────────────────────────────
      - name: Package (tar.gz)
        if: matrix.archive == 'tar.gz'
        shell: bash
        run: |
          VERSION="${GITHUB_REF_NAME}"
          TARGET="${{ matrix.target }}"
          DIR="garudust-${VERSION}-${TARGET}"
          mkdir -p "${DIR}"
          cp "target/${TARGET}/release/garudust"        "${DIR}/"
          cp "target/${TARGET}/release/garudust-server" "${DIR}/"
          cp README.md LICENSE                          "${DIR}/"
          tar -czf "${DIR}.tar.gz" "${DIR}"
          echo "ASSET=${DIR}.tar.gz" >> "$GITHUB_ENV"

      - name: Package (zip — Windows)
        if: matrix.archive == 'zip'
        shell: pwsh
        run: |
          $VERSION = $env:GITHUB_REF_NAME
          $TARGET  = "${{ matrix.target }}"
          $DIR     = "garudust-${VERSION}-${TARGET}"
          New-Item -ItemType Directory -Path $DIR | Out-Null
          Copy-Item "target\${TARGET}\release\garudust.exe"        "${DIR}\"
          Copy-Item "target\${TARGET}\release\garudust-server.exe" "${DIR}\"
          Copy-Item README.md, LICENSE                              "${DIR}\"
          Compress-Archive -Path "${DIR}\*" -DestinationPath "${DIR}.zip"
          "ASSET=${DIR}.zip" | Out-File -FilePath $env:GITHUB_ENV -Append

      - name: Upload artifact
        uses: actions/upload-artifact@v7
        with:
          name: ${{ matrix.target }}
          path: garudust-${{ github.ref_name }}-${{ matrix.target }}.*

  publish:
    name: Publish to crates.io
    needs: build
    runs-on: ubuntu-latest
    environment: crates-io

    steps:
      - uses: actions/checkout@v6

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

      - name: Cache cargo
        uses: actions/cache@v5
        with:
          path: |
            ~/.cargo/registry/index
            ~/.cargo/registry/cache
            ~/.cargo/git/db
            target
          key: publish-${{ hashFiles('**/Cargo.lock') }}
          restore-keys: publish-

      - name: Publish crates in dependency order
        env:
          CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
        run: |
          publish_or_skip() {
            local crate="$1"
            echo "Publishing $crate..."
            local out
            if out=$(cargo publish -p "$crate" --no-verify 2>&1); then
              echo "$out"
              sleep 20
            elif echo "$out" | grep -q "already exists"; then
              echo "  → $crate already published, skipping."
            else
              echo "$out"
              exit 1
            fi
          }
          publish_or_skip garudust-core
          publish_or_skip garudust-memory
          publish_or_skip garudust-transport
          publish_or_skip garudust-tools
          publish_or_skip garudust-agent
          publish_or_skip garudust-gateway
          publish_or_skip garudust-cron
          publish_or_skip garudust-platforms

      - name: Publish binaries
        env:
          CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
        run: |
          publish_or_skip() {
            local crate="$1"
            echo "Publishing $crate..."
            local out
            if out=$(cargo publish -p "$crate" --no-verify 2>&1); then
              echo "$out"
              sleep 20
            elif echo "$out" | grep -q "already exists"; then
              echo "  → $crate already published, skipping."
            else
              echo "$out"
              exit 1
            fi
          }
          publish_or_skip garudust
          publish_or_skip garudust-server

  release:
    name: Publish GitHub Release
    needs: build
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v6

      - name: Download all artifacts
        uses: actions/download-artifact@v8
        with:
          path: artifacts
          merge-multiple: true

      - name: Generate checksums
        run: |
          cd artifacts
          sha256sum * > SHA256SUMS.txt

      - name: Create Release
        uses: softprops/action-gh-release@v3
        with:
          files: |
            artifacts/*
          generate_release_notes: true
          draft: false
          prerelease: ${{ contains(github.ref_name, '-') }}
