name: Plugins/api-doc deployer

on:
  push:
    branches:
      - develop
      - staging
      - main
    paths:
      - 'plugins/libs/plugin-types/index.d.ts'
      - 'plugins/libs/plugin-types/REAME.md'
      - 'plugins/tools/typedoc.css'
      - 'plugins/CHANGELOG.md'
      - 'plugins/wrangler-penpot-plugins-api-doc.toml'
  workflow_dispatch:
    inputs:
      gh_ref:
        description: 'Name of the branch'
        type: choice
        required: true
        default: 'develop'
        options:
          - develop
          - staging
          - main

permissions:
  contents: read

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - name: Extract some useful variables
        id: vars
        run: |
          echo "gh_ref=${{ inputs.gh_ref || github.ref_name }}" >> $GITHUB_OUTPUT

      - name: Checkout
        uses: actions/checkout@v6
        with:
          fetch-depth: 0
          ref: ${{ steps.vars.outputs.gh_ref }}

      # START: Setup Node and PNPM enabling cache
      - name: Setup Node.js
        uses: actions/setup-node@v6
        with:
          node-version-file: .nvmrc

      - name: Enable PNPM
        working-directory: ./plugins
        shell: bash
        run: |
          corepack enable;
          corepack install;

      - name: Get pnpm store path
        id: pnpm-store
        working-directory: ./plugins
        shell: bash
        run: echo "STORE_PATH=$(pnpm store path --silent)" >> $GITHUB_OUTPUT

      - name: Cache pnpm store
        uses: actions/cache@v5
        with:
          path: ${{ steps.pnpm-store.outputs.STORE_PATH }}
          key: ${{ runner.os }}-pnpm-${{ hashFiles('plugins/pnpm-lock.yaml') }}
          restore-keys: |
            ${{ runner.os }}-pnpm-
      # END: Setup Node and PNPM enabling cache

      - name: Install deps
        working-directory: ./plugins
        shell: bash
        run: |
          pnpm install --no-frozen-lockfile;
          pnpm add -D -w wrangler@latest;

      - name: Build docs
        working-directory: plugins
        shell: bash
        run: pnpm run build:doc

      - name: Select Worker name
        run: |
          REF="${{ steps.vars.outputs.gh_ref }}"
          case "$REF" in
            main)
              echo "WORKER_NAME=penpot-plugins-api-doc-pro" >> $GITHUB_ENV
              echo "WORKER_URI=doc.plugins.penpot.app" >> $GITHUB_ENV ;;
            staging)
              echo "WORKER_NAME=penpot-plugins-api-doc-pre" >> $GITHUB_ENV
              echo "WORKER_URI=doc.plugins.penpot.dev" >> $GITHUB_ENV ;;
            develop)
              echo "WORKER_NAME=penpot-plugins-api-doc-hourly" >> $GITHUB_ENV
              echo "WORKER_URI=doc.plugins.hourly.penpot.dev" >> $GITHUB_ENV ;;
            *) echo "Unsupported branch ${REF}" && exit 1 ;;
          esac

      - name: Set the custom url
        working-directory: plugins
        shell: bash
        run: |
          sed -i "s/WORKER_URI/${{ env.WORKER_URI }}/g" wrangler-penpot-plugins-api-doc.toml

      - name: Add noindex header and robots.txt files for non-production environments
        if: ${{ steps.vars.outputs.gh_ref != 'main' }}
        working-directory: plugins
        shell: bash
        run: |
          ASSETS_DIR="dist/doc"

          cat > "${ASSETS_DIR}/_headers" << 'EOF'
          /*
            X-Robots-Tag: noindex, nofollow
          EOF

          cat > "${ASSETS_DIR}/robots.txt" << 'EOF'
          User-agent: *
          Disallow: /
          EOF

      - name: Deploy to Cloudflare Workers
        uses: cloudflare/wrangler-action@v3
        with:
          workingDirectory: plugins
          apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
          accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
          command: deploy --config wrangler-penpot-plugins-api-doc.toml --name ${{ env.WORKER_NAME }}

      - name: Notify Mattermost
        if: failure()
        uses: mattermost/action-mattermost-notify@ae31bb6f9e26a54336e79696f108a2c91cf55b4e # v2.1.0
        with:
          MATTERMOST_WEBHOOK_URL: ${{ secrets.MATTERMOST_WEBHOOK }}
          MATTERMOST_CHANNEL: bot-alerts-cicd
          TEXT: |
            ❌ 🧩📚 *[PENPOT PLUGINS] Error deploying API documentation.*
            📄 Triggered from ref: `${{ inputs.gh_ref }}`
            🔗 Run: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}
            @infra
