name: Docs

on:
  push:
    branches:
      - main
    paths:
      - 'site/**'
      - '.github/workflows/docs.yml'
      - 'src/runtime/**'

  workflow_dispatch:

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

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

jobs:
  build_ssg:
    runs-on: ubuntu-latest
    name: Build (SSG)
    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: Build Site
        run: cd site && zig build -Doptimize=ReleaseSmall

      - name: Export Site
        run: cd site && zig build zx -- export

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

  pre_deploy:
    name: Pre-deploy
    needs: build_ssg
    uses: ./.github/workflows/e2e.yml
    with:
      report_name: playwright-report-pre-deploy

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

  post_deploy:
    name: Post-deploy
    needs: [deploy_ssg, deploy_edge]
    uses: ./.github/workflows/e2e.yml
    with:
      base_url: ${{ needs.deploy_ssg.outputs.page_url }}
      report_name: playwright-report-post-deploy

  deploy_ssr:
    environment:
      name: ssr
      url: https://ssr.ziex.dev
    runs-on: self-hosted
    name: Deploy (SSR)
    needs: [pre_deploy]
    steps:
      - name: Checkout
        uses: actions/checkout@v4

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

  deploy_edge:
    environment:
      name: edge
      url: https://site.ziex.workers.dev
    runs-on: ubuntu-latest
    name: Deploy (Edge)
    needs: [pre_deploy]
    steps:
      - 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: Install Node Dependencies
        run: cd site && bun install

      - name: Build Site
        run: cd site && zig build

      - name: Deploy to Edge
        uses: cloudflare/wrangler-action@v3
        with:
          apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
          accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
          workingDirectory: ./site


