name: Deploy dtact to GitHub Pages

# Merges the old benchmark.yml (Criterion + gh-pages) and the web deploy workflow.
# - build-bench: runs cargo bench, uploads criterion report as artifact
# - build-web:   trunk build --release, uploads dist/ as artifact
# - deploy:      downloads both artifacts, nests bench/ under dist/, then
#                pushes a single GitHub Pages deployment
#                  /          → dtact web frontend
#                  /bench/    → Criterion HTML report

on:
  push:
    branches: [main]
    paths:
      - "web/**"
      - "logo.svg"
      - "src/**"
      - "benches/**"
      - "dtact-macros/**"
      - ".github/workflows/deploy.yml"
  workflow_dispatch:

permissions:
  contents: read
  pages: write
  id-token: write

concurrency:
  group: pages
  cancel-in-progress: true

jobs:
  # ── Criterion benchmarks ─────────────────────────────────────────────────
  build-bench:
    name: Run Benchmarks
    runs-on: ubuntu-latest
    steps:
      - name: Harden the runner (Audit all outbound calls)
        uses: step-security/harden-runner@9af89fc71515a100421586dfdb3dc9c984fbf411 # v2.19.4
        with:
          egress-policy: audit

      - uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0

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

      - name: Cache Cargo registry + build
        uses: Swatinem/rust-cache@e18b497796c12c097a38f9edb9d0641fb99eee32 # v2

      - name: Run Criterion benchmarks
        run: cargo bench

      - name: Upload criterion report artifact
        uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
        with:
          name: criterion-report
          path: target/criterion
          retention-days: 7

  # ── Leptos/WASM web frontend ─────────────────────────────────────────────
  build-web:
    name: Build Web Frontend
    runs-on: ubuntu-latest
    steps:
      - name: Harden the runner (Audit all outbound calls)
        uses: step-security/harden-runner@9af89fc71515a100421586dfdb3dc9c984fbf411 # v2.19.4
        with:
          egress-policy: audit

      - uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0

      - name: Install Rust + wasm32 target
        uses: dtolnay/rust-toolchain@29eef336d9b2848a0b548edc03f92a220660cdb8 # stable
        with:
          targets: wasm32-unknown-unknown

      - name: Cache Cargo registry + web build
        uses: Swatinem/rust-cache@e18b497796c12c097a38f9edb9d0641fb99eee32 # v2
        with:
          workspaces: web

      - name: Install Trunk
        run: cargo install trunk --locked

      - name: Build Leptos app
        working-directory: web
        run: |
          export TRUNK_TOOLS_WASM_BINDGEN="0.2.125"
          trunk build --release -v

      - name: Upload web dist artifact
        uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
        with:
          name: web-dist
          path: web/dist
          retention-days: 7

  # ── Combine + deploy ─────────────────────────────────────────────────────
  deploy:
    name: Deploy to GitHub Pages
    runs-on: ubuntu-latest
    needs: [build-bench, build-web]
    environment:
      name: github-pages
      url: ${{ steps.deployment.outputs.page_url }}
    steps:
      - name: Harden the runner (Audit all outbound calls)
        uses: step-security/harden-runner@9af89fc71515a100421586dfdb3dc9c984fbf411 # v2.19.4
        with:
          egress-policy: audit

      - name: Download web dist
        uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
        with:
          name: web-dist
          path: dist

      - name: Download Criterion report
        uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
        with:
          name: criterion-report
          path: dist/bench

      - name: Upload combined Pages artifact
        uses: actions/upload-pages-artifact@fc324d3547104276b827a68afc52ff2a11cc49c9 # v5.0.0
        with:
          path: dist

      - name: Deploy to GitHub Pages
        id: deployment
        uses: actions/deploy-pages@cd2ce8fcbc39b97be8ca5fce6e763baed58fa128 # v5.0.0
