name: Deploy Nango Integrations
'on':
    # To deploy nango integrations automatically on code changes:
    # - uncomment the push option below
    # - set the appropriate branch
    # - uncomment the elif at the bottom of this file to deploy to the appropriate stage
    # push:
    #     branches:
    #         - main
    workflow_dispatch:
        inputs:
            stage:
                type: choice
                description: 'stage to deploy to, defaults to dev'
                required: true
                default: 'dev'
                options:
                    - dev
                    - prod
            allowDestructive:
                type: choice
                description: 'deploy destructive changes without confirmation'
                required: true
                default: false
                options:
                    - true
                    - false
jobs:
    deploy:
        runs-on: ubuntu-latest
        env:
            NANGO_SECRET_KEY_DEV: ${{ secrets.NANGO_SECRET_KEY_DEV }}
            NANGO_SECRET_KEY_PROD: ${{ secrets.NANGO_SECRET_KEY_PROD }}
        steps:
            - name: Checkout code
              uses: actions/checkout@v4
            - name: Install Nango CLI
              run: npm install nango -g
            - name: run Nango deploy
              run: |
                  cd nango-integrations
                  if [ "${{ github.event_name }}" == "workflow_dispatch" ]; then
                      stage="${{ inputs.stage }}"
                  # elif [ "${{ github.ref }}" == "refs/heads/main" ]; then
                  #     stage="prod"
                  else
                      stage="dev"
                  fi

                  STAGE_UPPER=$(echo "$stage" | tr '[:lower:]' '[:upper:]')

                  SECRET_KEY="${{ secrets[SECRET_KEY_VAR] }}"

                  if [ -z "$SECRET_KEY" ]; then
                      echo "Error: ${SECRET_KEY_VAR} secret is not set"
                      exit 1
                  fi

                  nango deploy "$stage" --auto-confirm ${{ inputs.allowDestructive == 'true' && '--allow-destructive' || '' }}