name: Playwright Account Setup and Demo Data Generation

on:
  workflow_dispatch:
    inputs:
      environment:
        description: 'Target environment (dev, stg, prod)'
        required: true
        default: 'dev'
        type: choice
        options:
          - dev
          - stg
          - prod
      ACCT_NAME:
        description: 'Provide a name for the account if you want to create a new account'
        required: false
        type: string
      ADMIN_KEY:
        description: 'Provide a valid Admin Key for new account creation'
        required: false
        type: string
      ADMIN_SECRET:
        description: 'Provide a valid Admin Secret for new account creation'
        required: false
        type: string
      AUTH_KEY:
        description: 'Provide a valid Auth Key for existing account'
        required: false
        type: string
      AUTH_SECRET:
        description: 'Provide a valid Auth Secret for existing account'
        required: false
        type: string      
      DataSuffix:
        description: 'Suffix for entity names(Do not exceed 8 characters)'
        required: false
        type: string
      ORDER_CREATE_COUNT:
        description: 'Provide number of orders to create else default to 30'
        required: false
        type: string
      ORDER_DISPATCH_COUNT:
        description: 'Provide number of orders to dispatch else default to 10'
        required: false
        type: string
      USER_INVITE_EMAILS:
        description: 'Comma separated emails for TTC user invite (optional)'
        required: false
        type: string

jobs:
  run-demo-data-tests:
    runs-on: ubuntu-latest
    env:
      INCLUDE_DEMO_DATA_TESTS: 'true'
      NODE_VERSION: lts/*
      WORKING_DIR: ./Playwright
      DATA_SUFFIX: ${{ github.event.inputs.DataSuffix }}
      ORDER_CREATE_COUNT: ${{ github.event.inputs.ORDER_CREATE_COUNT }}
      ORDER_DISPATCH_COUNT: ${{ github.event.inputs.ORDER_DISPATCH_COUNT }}
      USER_INVITE_EMAILS: ${{ github.event.inputs.USER_INVITE_EMAILS }}
      ACCT_NAME: ${{ github.event.inputs.ACCT_NAME }}
      ADMIN_KEY: ${{ github.event.inputs.ADMIN_KEY }}
      ADMIN_SECRET: ${{ github.event.inputs.ADMIN_SECRET }}
      AUTH_KEY: ${{ github.event.inputs.AUTH_KEY }}
      AUTH_SECRET: ${{ github.event.inputs.AUTH_SECRET }}
      BASE_URL: ${{ github.event.inputs.environment == 'dev' && 'https://cloud.dev.api.trimblecloud.com/transportation' || github.event.inputs.environment == 'stg' && 'https://cloud.stage.api.trimblecloud.com/transportation' || 'https://cloud.api.trimble.com/transportation' }}
      AUTH_URL: ${{ github.event.inputs.environment == 'dev' && 'https://cloud.dev.api.trimblecloud.com/transportation/api/identity/v2/oauth2/token' || github.event.inputs.environment == 'stg' && 'https://cloud.stage.api.trimblecloud.com/transportation/api/identity/v2/oauth2/token' || 'https://cloud.api.trimble.com/transportation/api/identity/v2/oauth2/token' }}
      ENVIRONMENT: ${{ github.event.inputs.environment }}
      AUTHORIZATION_URL: ${{ github.event.inputs.environment == 'dev' && 'https://cloud.dev.api.trimblecloud.com/transportation/api/identity/v2/oauth2/token' || github.event.inputs.environment == 'stg' && 'https://cloud.stage.api.trimblecloud.com/transportation/api/identity/v2/oauth2/token' || 'https://cloud.api.trimble.com/transportation/api/identity/v2/oauth2/token' }}
      CREATE_ACCOUNT_URL: ${{ github.event.inputs.environment == 'dev' && 'https://cloud.dev.api.trimblecloud.com/transportation/api/core/v2/accounts' || github.event.inputs.environment == 'stg' && 'https://cloud.stage.api.trimblecloud.com/transportation/api/core/v2/accounts' || 'https://cloud.api.trimble.com/transportation/api/core/v2/accounts' }}
      CREATE_APPLICATION_URL: ${{ github.event.inputs.environment == 'dev' && 'https://cloud.dev.api.trimblecloud.com/transportation/iam/v2/applications' || github.event.inputs.environment == 'stg' && 'https://cloud.stage.api.trimblecloud.com/transportation/iam/v2/applications' || 'https://cloud.api.trimble.com/transportation/iam/v2/applications' }}
      CREATE_APPLICATION_PERMISSIONS_URL: ${{ github.event.inputs.environment == 'dev' && 'https://cloud.dev.api.trimblecloud.com/transportation/iam/v2/applications/' || github.event.inputs.environment == 'stg' && 'https://cloud.stage.api.trimblecloud.com/transportation/iam/v2/applications/' || 'https://cloud.api.trimble.com/transportation/iam/v2/applications/' }}
      ONBOARD_ACCOUNT_URL: ${{ github.event.inputs.environment == 'dev' && 'https://cloud.dev.api.trimblecloud.com/transportation/fds/v1/accounts/onboard' || github.event.inputs.environment == 'stg' && 'https://cloud.stage.api.trimblecloud.com/transportation/fds/v1/accounts/onboard' || 'https://cloud.api.trimble.com/transportation/fds/v1/accounts/onboard' }}
      CREATE_USER_INVITE_URL: ${{ github.event.inputs.environment == 'dev' && 'https://cloud.dev.api.trimblecloud.com/transportation/iam/v2/user-invites' || github.event.inputs.environment == 'stg' && 'https://cloud.stage.api.trimblecloud.com/transportation/iam/v2/user-invites' || 'https://cloud.api.trimble.com/transportation/iam/v2/user-invites' }}
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Validate required inputs
        run: |
          if [[ -n "${{ env.ACCT_NAME }}" || -n "${{ env.ADMIN_KEY }}" || -n "${{ env.ADMIN_SECRET }}" ]]; then
            if [[ -z "${{ env.ACCT_NAME }}" || -z "${{ env.ADMIN_KEY }}" || -z "${{ env.ADMIN_SECRET }}" ]]; then
              echo "Error: To create a new account, you must provide ACCT_NAME, ADMIN_KEY, and ADMIN_SECRET."
              exit 1
            fi
          elif [[ -n "${{ env.AUTH_KEY }}" || -n "${{ env.AUTH_SECRET }}" ]]; then
            if [[ -z "${{ env.AUTH_KEY }}" || -z "${{ env.AUTH_SECRET }}" ]]; then
              echo "Error: To use an existing account, you must provide both AUTH_KEY and AUTH_SECRET."
              exit 1
            fi
          else
            echo "Error: You must provide either (ACCT_NAME, ADMIN_KEY, ADMIN_SECRET) for new account or (AUTH_KEY, AUTH_SECRET) for existing account."
            exit 1
          fi
        shell: bash
      
      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: ${{ env.NODE_VERSION }}

      - name: Install dependencies
        run: npm ci
        working-directory: ${{ env.WORKING_DIR }}

      - name: Run TTC Account Creation Test
        if: ${{ env.ACCT_NAME != '' && env.ADMIN_KEY != '' && env.ADMIN_SECRET != '' }}
        run: |
          npx playwright test tests/demo-data/TtcAccount/create-ttcAccount.spec.ts
        working-directory: ${{ env.WORKING_DIR }}
        env:
          INCLUDE_DEMO_DATA_TESTS: 'true'
          BASE_URL: ${{ env.BASE_URL }}
          AUTH_URL: ${{ env.AUTH_URL }}
          AUTH_KEY: ${{ env.ADMIN_KEY }}
          AUTH_SECRET: ${{ env.ADMIN_SECRET }}
          ACCT_NAME: ${{ env.ACCT_NAME }}
          ENVIRONMENT: ${{ env.ENVIRONMENT }}
          AUTHORIZATION_URL: ${{ env.AUTHORIZATION_URL }}
          CREATE_ACCOUNT_URL: ${{ env.CREATE_ACCOUNT_URL }}
          CREATE_APPLICATION_URL: ${{ env.CREATE_APPLICATION_URL }}
          CREATE_APPLICATION_PERMISSIONS_URL: ${{ env.CREATE_APPLICATION_PERMISSIONS_URL }}
          ONBOARD_ACCOUNT_URL: ${{ env.ONBOARD_ACCOUNT_URL }}
          CREATE_USER_INVITE_URL: ${{ env.CREATE_USER_INVITE_URL }}
          USER_INVITE_EMAILS: ${{ env.USER_INVITE_EMAILS }}

      - name: Extract TTC Credentials using Node.js
        if: ${{ env.ACCT_NAME != '' && env.ADMIN_KEY != '' && env.ADMIN_SECRET != '' }}
        run: |
          AUTH_KEY=$(node -p 'require("./tests/demo-data/TtcAccount/ttc-latest-credentials.json").authKey')
          echo "AUTH_KEY=$AUTH_KEY" >> $GITHUB_ENV
          AUTH_SECRET=$(node -p 'require("./tests/demo-data/TtcAccount/ttc-latest-credentials.json").authSecret')
          echo "AUTH_SECRET=$AUTH_SECRET" >> $GITHUB_ENV
        working-directory: ${{ env.WORKING_DIR }}

      - name: Run Playwright Permanent demo-data tests - Partners
        if: ${{ env.ACCT_NAME == '' || (env.ACCT_NAME != '' && env.ADMIN_KEY != '' && env.ADMIN_SECRET != '') }}
        run: |
          npx playwright test \
            tests/demo-data/Partners/create-partner.spec.ts
        working-directory: ${{ env.WORKING_DIR }}
        env:
          INCLUDE_DEMO_DATA_TESTS: 'true'
          BASE_URL: ${{ env.BASE_URL }}
          AUTH_URL: ${{ env.AUTH_URL }}
          AUTH_KEY: ${{ env.AUTH_KEY }}
          AUTH_SECRET: ${{ env.AUTH_SECRET }}
          DATA_SUFFIX: ${{ env.DATA_SUFFIX }}
          ENVIRONMENT: ${{ env.ENVIRONMENT }}
          ACCT_NAME: ${{ env.ACCT_NAME }}
          ORDER_CREATE_COUNT: ${{ env.ORDER_CREATE_COUNT }}
          ORDER_DISPATCH_COUNT: ${{ env.ORDER_DISPATCH_COUNT }}
          AUTHORIZATION_URL: ${{ env.AUTHORIZATION_URL }}       

      - name: Run Playwright Permanent demo-data tests - Drivers, PowerUnits, CargoUnits
        if: ${{ env.ACCT_NAME == '' || (env.ACCT_NAME != '' && env.ADMIN_KEY != '' && env.ADMIN_SECRET != '') }}
        run: |
          npx playwright test \
            tests/demo-data/PowerUnit/create-powerunit.spec.ts \
            tests/demo-data/CargoUnit/create-cargounit.spec.ts \
            tests/demo-data/Drivers/create-driver.spec.ts
        working-directory: ${{ env.WORKING_DIR }}
        env:
          INCLUDE_DEMO_DATA_TESTS: 'true'
          BASE_URL: ${{ env.BASE_URL }}
          AUTH_URL: ${{ env.AUTH_URL }}
          AUTH_KEY: ${{ env.AUTH_KEY }}
          AUTH_SECRET: ${{ env.AUTH_SECRET }}
          DATA_SUFFIX: ${{ env.DATA_SUFFIX }}
          ENVIRONMENT: ${{ env.ENVIRONMENT }}
          ACCT_NAME: ${{ env.ACCT_NAME }}
          ORDER_CREATE_COUNT: ${{ env.ORDER_CREATE_COUNT }}
          ORDER_DISPATCH_COUNT: ${{ env.ORDER_DISPATCH_COUNT }}
          AUTHORIZATION_URL: ${{ env.AUTHORIZATION_URL }}

      - name: Run Playwright Permanent demo-data tests - Orders (daily-order-lifecycle)
        if: ${{ env.ACCT_NAME == '' || (env.ACCT_NAME != '' && env.ADMIN_KEY != '' && env.ADMIN_SECRET != '') }}
        run: |
          npx playwright test \
            tests/demo-data/Orders/daily-order-lifecycle.spec.ts
        working-directory: ${{ env.WORKING_DIR }}
        env:
          INCLUDE_DEMO_DATA_TESTS: 'true'
          BASE_URL: ${{ env.BASE_URL }}
          AUTH_URL: ${{ env.AUTH_URL }}
          AUTH_KEY: ${{ env.AUTH_KEY }}
          AUTH_SECRET: ${{ env.AUTH_SECRET }}
          DATA_SUFFIX: ${{ env.DATA_SUFFIX }}
          ENVIRONMENT: ${{ env.ENVIRONMENT }}
          ACCT_NAME: ${{ env.ACCT_NAME }}
          ORDER_CREATE_COUNT: ${{ env.ORDER_CREATE_COUNT }}
          ORDER_DISPATCH_COUNT: ${{ env.ORDER_DISPATCH_COUNT }}
          AUTHORIZATION_URL: ${{ env.AUTHORIZATION_URL }}