name: Deploy application
description: Deploy an app's backend and frontend to a Tailor Platform workspace.

# Requires: pnpm install already run. Uses the app's own deploy scripts.

inputs:
  token:
    description: "Tailor Platform access token (e.g. the `token` output of erp-kit-fetch-tailor-token)."
    required: true
  workspace_id:
    description: "Tailor workspace ID to deploy into."
    required: true
  app:
    description: "App name under apps/ (e.g. user-management)."
    required: true

outputs:
  app_url:
    description: "Backend app URL (the VITE_TAILOR_APP_URL the frontend was built against)."
    value: ${{ steps.vars.outputs.app_url }}
  deployed_url:
    description: "Deployed frontend (static website) URL."
    value: ${{ steps.frontend.outputs.deployed_url }}

runs:
  using: composite
  steps:
    - name: Deploy backend
      shell: bash
      env:
        TAILOR_PLATFORM_TOKEN: ${{ inputs.token }}
        TAILOR_PLATFORM_WORKSPACE_ID: ${{ inputs.workspace_id }}
        APP: ${{ inputs.app }}
      run: pnpm -C "apps/$APP/backend" run --silent deploy --yes

    - name: Resolve app URL and OAuth client
      id: vars
      shell: bash
      env:
        TAILOR_PLATFORM_TOKEN: ${{ inputs.token }}
        TAILOR_PLATFORM_WORKSPACE_ID: ${{ inputs.workspace_id }}
        APP: ${{ inputs.app }}
      run: |
        set -euo pipefail
        app_url="$(pnpm -C "apps/$APP/backend" exec tailor show --json | jq -r '.url')"
        # Prefer the client named "default"; fall back to the first client.
        client_id="$(pnpm -C "apps/$APP/backend" exec tailor oauth2client list --json \
          | jq -r '(map(select(.name == "default")) | first | .clientId) // (.[0].clientId) // ""')"
        if [ -z "$app_url" ] || [ "$app_url" = "null" ]; then
          echo "::error::Could not resolve the app URL for $APP"
          exit 1
        fi
        if [ -z "$client_id" ] || [ "$client_id" = "null" ]; then
          echo "::error::Could not resolve an OAuth2 client id for $APP"
          exit 1
        fi
        echo "app_url=$app_url" >> "$GITHUB_OUTPUT"
        echo "client_id=$client_id" >> "$GITHUB_OUTPUT"

    - name: Build frontend
      shell: bash
      env:
        VITE_TAILOR_APP_URL: ${{ steps.vars.outputs.app_url }}
        VITE_TAILOR_CLIENT_ID: ${{ steps.vars.outputs.client_id }}
        # The build is memory-hungry; give it headroom.
        NODE_OPTIONS: --max-old-space-size=6144
        APP: ${{ inputs.app }}
      run: pnpm -C "apps/$APP/frontend" run build

    - name: Deploy frontend
      id: frontend
      shell: bash
      env:
        TAILOR_PLATFORM_TOKEN: ${{ inputs.token }}
        TAILOR_PLATFORM_WORKSPACE_ID: ${{ inputs.workspace_id }}
        APP: ${{ inputs.app }}
      run: |
        set -euo pipefail
        url="$(pnpm -C "apps/$APP/backend" run --silent deploy:frontend --json | jq -r '.url')"
        if [ -z "$url" ] || [ "$url" = "null" ]; then
          echo "::error::Frontend deploy did not return a URL for $APP"
          exit 1
        fi
        echo "deployed_url=$url" >> "$GITHUB_OUTPUT"
