name: Build, Test, and Release

on:
  push:
    branches:
      - main
      - master
      - next
  pull_request:
    branches:
      - main
      - master

permissions:
  contents: write
  issues: write
  pull-requests: write
  pages: write
  id-token: write

# Allow one concurrent deployment
concurrency:
  group: "pages"
  cancel-in-progress: true

jobs:
  test:
    name: Test Rust Library
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      
      - name: Install Rust Toolchain
        uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: stable
          override: true
          target: wasm32-unknown-unknown
      
      - name: Run tests
        run: cargo test

  build:
    name: Build Demo App
    needs: test
    if: github.event_name == 'push'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      
      - name: Restore cache
        uses: actions/cache@v3
        with:
          path: |
            ~/.cargo/bin
            ~/.cargo/git
            ~/.cargo/registry
            target
          key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
      
      - name: Install Trunk
        uses: jetli/trunk-action@v0.4.0
        with:
          version: 'latest'
      
      - name: Add WASM target
        run: rustup target add wasm32-unknown-unknown
      
      - name: Build Demo App
        run: |
          cd demo
          RUSTFLAGS='--cfg getrandom_backend="wasm_js"' trunk build --public-url "/bigcolor" --release
      
      - name: Setup Pages
        uses: actions/configure-pages@v5
      
      - name: Upload artifact
        uses: actions/upload-pages-artifact@v3
        with:
          path: "demo/dist"

  deploy:
    name: Deploy to GitHub Pages
    needs: build
    if: github.event_name == 'push'
    runs-on: ubuntu-latest
    
    permissions:
      pages: write
      id-token: write
    
    environment:
      name: github-pages
      url: ${{ steps.deployment.outputs.page_url }}
    
    steps:
      - name: Deploy to GitHub Pages
        id: deployment
        uses: actions/deploy-pages@v4
  
  release:
    name: Semantic Release
    needs: [test, deploy]
    if: github.event_name == 'push'
    runs-on: ubuntu-latest
    permissions:
      contents: write
      issues: write
      pull-requests: write
    
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0
      
      - name: Setup Node
        uses: actions/setup-node@v4
        with:
          node-version: "lts/*"
      
      - name: Install dependencies
        run: npm install
      
      - name: Setup Rust
        uses: dtolnay/rust-toolchain@stable
      - name: Update Rust toolchain
        run: rustup update stable
      - name: Install cargo-edit
        run: cargo install cargo-edit

      - name: Release
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
        run: npx semantic-release