on:
  push:
    branches:
      - master

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Cache dependencies
        uses: actions/cache@v4
        with:
          path: |
            ~/.cargo/
            ~/.cargo/bin/
            ~/.cache/trunk/
            target/
            node_modules/
          key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
          restore-keys: ${{ runner.os }}-cargo-wordl

      - name: Install Rust
        uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: stable
          
      - name: Add Rust target wasm32-unknown-unknown
        run: rustup target add wasm32-unknown-unknown
      
      - name: Install Trunk
        run: cargo install trunk

      - name: Install Node.js
        uses: actions/setup-node@v4
        with:
          node-version: 20

      - name: Install Tailwind CSS
        run: npm install -g tailwindcss

      - name: Build Frontend
        working-directory: ./frontend/
        run: |
          trunk build --release

      - name: Commit Frontend Release Build
        run: |
          git config --global user.name "github-actions"
          git config --global user.email "dev@itsscb.de"
          git add -A
          git commit -m "release: frontend"
          git push

      - name: Upload Frontend Artifacts
        uses: actions/upload-artifact@v4
        with:
          name: frontend-artifacts
          path: ./frontend/dist


      - name: Build and test code
        run: |
          cargo build --verbose --release
          cargo test --verbose

  deploy:
    runs-on: ubuntu-latest
    needs: build
    steps:
      - name: Download Frontend Artifacts
        uses: actions/download-artifact@v4
        with:
          name: frontend-artifacts

      - name: Checkout
        uses: actions/checkout@v4

      - uses: shuttle-hq/deploy-action@main
        with:
          deploy-key: ${{ secrets.SHUTTLE_API_KEY }}