name: Deploy static site

on:
  push:
    branches:
      - main

env:
  NODE_VERSION: "lts/*"

# Sets permissions for GitHub Pages deployment
permissions:
  contents: read
  pages: write
  id-token: write

jobs:
  test:
    runs-on: ubuntu-latest
    if: github.ref == 'refs/heads/main' || github.event_name == 'pull_request'
    steps:
      - uses: actions/checkout@v4

      - name: Setup Node
        uses: actions/setup-node@v4
        with:
          node-version: ${{ env.NODE_VERSION }}
          cache: "npm"

      - name: Install dependencies
        run: npm ci

      - name: Run tests
        run: npm run test

      - name: Run typecheck
        run: npm run typecheck

  build-site:
    runs-on: ubuntu-latest
    if: github.ref == 'refs/heads/main'
    needs: test
    environment:
      name: github-pages
      url: ${{ steps.deployment.outputs.page_url }}
    env:
      VITE_BASE: "/"
    steps:
      - uses: actions/checkout@v4

      - name: Setup Node
        uses: actions/setup-node@v4
        with:
          node-version: ${{ env.NODE_VERSION }}
          cache: "npm"

      - name: Install dependencies
        run: npm ci

      - name: Build site, Storybook, and Typedoc
        run: npm run build

      - name: Prepare public folder
        run: |
          rm -rf public
          mv dist-website public

      - name: Upload Pages artifact
        uses: actions/upload-pages-artifact@v3
        with:
          path: public/

      - name: Deploy to GitHub Pages
        id: deployment
        uses: actions/deploy-pages@v4
