name: Docs

on:
  push:
    branches:
      - main
    paths:
      - 'site/**'

  workflow_dispatch:

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

concurrency:
  group: 'pages'
  cancel-in-progress: false

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

      - name: Setup Zig
        uses: mlugg/setup-zig@v2
        with:
          version: 0.15.2

      - name: Setup Bun
        uses: oven-sh/setup-bun@v1
        with:
          bun-version: 1.3.5

      - name: Setup Pages
        uses: actions/configure-pages@v5

      - name: Install Node Dependencies
        run: cd site && bun install

      - name: Cache Playground Artifacts
        id: cache-playground
        uses: actions/cache@v4
        with:
          path: site/assets/playground
          key: playground-assets-${{ hashFiles('pkg/playground/**') }}
          restore-keys: |
            playground-assets-

      - name: Build Playground Artifacts
        if: steps.cache-playground.outputs.cache-hit != 'true'
        run: cd pkg/playground && zig build
        
      - name: Build Site
        run: zig build -Ddoc -Doptimize=ReleaseSmall

      - name: Export Site
        run: zig build export -Ddoc -Doptimize=ReleaseSmall

      - name: Upload artifact
        uses: actions/upload-pages-artifact@v3
        with:
          path: ./dist

  deploy_ssg:
    environment:
      name: github-pages
      url: ${{ steps.deployment.outputs.page_url }}
    runs-on: ubuntu-latest
    needs: build_ssg
    steps:
      - name: Deploy to GitHub Pages
        id: deployment
        uses: actions/deploy-pages@v4

  deploy_ssr:
    environment:
      name: ssr
      url: https://ssr.ziex.dev
    runs-on: self-hosted
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Deploy ssr.ziex.dev
        run: bash tools/docdeploy