name: pl-fe CI

on:
  push:
    branches: [ "develop" ]
  pull_request:
    branches: [ "develop" ]

jobs:
  build:
    runs-on: ubuntu-latest
    name: Test and upload artifacts
    strategy:
      matrix:
        node-version: [22.x]

    steps:
    - name: Install system dependencies
      run: sudo apt install -y unzip

    - name: Checkout code
      uses: actions/checkout@v3

    - name: Setup pnpm
      uses: pnpm/action-setup@v4
      with:
        version: 10

    - name: Use Node.js ${{ matrix.node-version }}
      uses: actions/setup-node@v3
      with:
        node-version: ${{ matrix.node-version }}
        cache: 'pnpm'

    - name: Setup Python
      uses: actions/setup-python@v5
      with:
        python-version: 3.x

    - name: Install deps
      working-directory: .
      run: pnpm install

    - name: Build pl-api
      env:
        NODE_ENV: production
      working-directory: ./packages/pl-api
      run: pnpm build

    - name: Lint
      working-directory: ./packages/pl-fe
      run: pnpm lint

    - name: Build
      env:
        NODE_ENV: production
      working-directory: ./packages/pl-fe
      run: pnpm build

    - name: i18n
      working-directory: ./packages/pl-fe
      run: |
          pnpm i18n
          git diff
          git diff --quiet || (echo "Locale files are out of date. Please run \`pnpm i18n\`" && exit 1)

    - name: Upload artifact
      uses: actions/upload-artifact@v4
      with:
        name: pl-fe
        path: packages/pl-fe/dist

    - name: Include archive
      working-directory: ./packages/pl-fe/dist
      run: zip -r ../pl-fe.zip .

    - name: build
      env:
        NODE_ENV: production
      working-directory: ./packages/pl-fe
      run: |
          WITH_LANDING_PAGE=true pnpm build
          cp dist/index.html dist/404.html
          cp pl-fe.zip dist/pl-fe.zip

    - name: Build pl-api documentation
      working-directory: ./packages/pl-api
      run: npx typedoc

    - name: Copy pl-api documentation
      working-directory: ./packages/pl-api
      run: cp docs ../pl-fe/dist/pl-api-docs -r

    - name: Build documentation
      working-directory: .
      run: pip install mkdocs-material mkdocs-include-markdown-plugin && mkdocs build

    - name: Copy documentation
      working-directory: .
      run: cp site ./packages/pl-fe/dist/docs -r

    - name: Upload Github Pages artifact
      uses: actions/upload-pages-artifact@v3
      with:
        name: github-pages
        path: packages/pl-fe/dist

  deploy:
    needs: build
    if: github.ref == 'refs/heads/develop'

    permissions:
      pages: write
      id-token: write

    environment:
      name: github-pages
      url: ${{ steps.deployment.outputs.page_url }}

    runs-on: ubuntu-latest
    steps:
      - name: Deploy to GitHub Pages
        id: deployment
        uses: actions/deploy-pages@v4

