# SPDX-FileCopyrightText: 2023 - 2024 Ali Sajid Imami
#
# SPDX-License-Identifier: Apache-2.0
# SPDX-License-Identifier: MIT

---
name: Book Deployment
on:
  workflow_call:
    outputs:
      deployment_url:
        description: "The vercel deployment url of the book"
        value: ${{ jobs.deploy_book.outputs.deployment_url }}

env:
  VERCEL_ORG_ID: ${{ secrets.VERCEL_ORG_ID }}
  VERCEL_PROJECT_ID: ${{ secrets.VERCEL_PROJECT_ID }}
  VERCEL_TOKEN: ${{ secrets.VERCEL_TOKEN }}
  GITHUB_TOKEN: ${{ secrets.ACTIONS_PAT_GITHUB }}
  BRANCH: ${{ github.ref_name }}

jobs:
  build_book:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout the Repository
        uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
      - name: Install mdbook
        uses: taiki-e/install-action@37461a1de4134bec919a737ee9ba018e72011b7c # v2.42.4
        with:
          tool: mdbook
      - name: Build the Book
        run: mdbook build guide
      - name: Upload artifact
        uses: actions/upload-artifact@0b2256b8c012f0828dc542b3febcab082c67f72b # v4.3.4
        with:
          name: book
          path: guide/book
          retention-days: 1
          compression-level: 9
  deploy_book:
    runs-on: ubuntu-latest
    needs: [build_book]
    env:
      PROJECT_ENVIRONMENT: ''
      STORE_PATH: ''
    outputs:
      deployment_url: ${{ steps.output_deployment_url.outputs.deployment_url }}
    steps:
      - name: Decide Deployment Environment
        id: deployment_environment
        run: |
          if [[ ${{ env.BRANCH }} == "main" ]]; then
            echo "PROJECT_ENVIRONMENT=production" >> $GITHUB_ENV
          else
            echo "PROJECT_ENVIRONMENT=preview" >> $GITHUB_ENV
          fi
      - name: Download artifact
        uses: actions/download-artifact@fa0a91b85d4f404e444e00e005971372dc801d16 # v4.1.8
        with:
          name: book
          path: /opt/book
      - name: Setup NodeJS
        uses: actions/setup-node@1e60f620b9541d16bece96c5465dc8ee9832be0b # v4.0.3
        with:
          node-version: '18'
      - name: Install pnpm
        uses: pnpm/action-setup@fe02b34f77f8bc703788d5817da081398fad5dd2 # v4.0.0
        with:
          version: 8
          run_install: false
      - name: Install pnpm dependencies
        run: pnpm install -g vercel@latest
      - name: Populate Vercel Variables
        run: vercel pull --cwd /opt/book --yes --environment=${{env.PROJECT_ENVIRONMENT}} --token=${{ env.VERCEL_TOKEN }}
      - name: Build for Vercel deployment -- Preview
        if: env.PROJECT_ENVIRONMENT == 'preview'
        run: vercel build --cwd /opt/book -y -d --token=${{ env.VERCEL_TOKEN }}
      - name: Deploy to Vercel -- Preview
        if: env.PROJECT_ENVIRONMENT == 'preview'
        run: vercel deploy --cwd /opt/book --prebuilt --token=${{ env.VERCEL_TOKEN }} > deployment_url
      - name: Build for Vercel deployment -- Production
        if: env.PROJECT_ENVIRONMENT == 'production'
        run: vercel build --cwd /opt/book -y --prod -d --token=${{ env.VERCEL_TOKEN }}
      - name: Deploy to Vercel -- Production
        if: env.PROJECT_ENVIRONMENT == 'production'
        run: vercel deploy --cwd /opt/book --prod --prebuilt --token=${{ env.VERCEL_TOKEN }} > deployment_url
      - name: Output Deployment URL
        id: output_deployment_url
        run: echo "deployment_url=$(cat deployment_url)" >> $GITHUB_OUTPUT
