name: PR Check

on:
  pull_request:
    types: [opened, synchronize, reopened]

jobs:
  check:
    runs-on: ubuntu-latest
    steps:  
      - name: Checkout repository code
        uses: actions/checkout@v4

      - name: Configure Git user
        run: |
          git config --global user.email "psdevteamenterprise@gmail"
          git config --global user.name "psdevteamenterprise"

      - name: Set up Node.js 20
        uses: actions/setup-node@v4
        with:
          node-version: 20

      - name: Authenticate with npm using PR token
        run: echo "//registry.npmjs.org/:_authToken=${{ secrets.NPM_TOKEN_PUBLISH }}" > .npmrc
      
      - name: Authenticate with GitHub Packages
        run: |
          echo "@psdevteamenterprise:registry=https://npm.pkg.github.com" >> ~/.npmrc
          echo "//npm.pkg.github.com/:_authToken=${{ secrets.PS_DEV_TEAM_GITHUB_TOKEN }}" >> ~/.npmrc

      - name: Install dependencies in root
        run: npm ci

      - name: Install Wix CLI globally
        run: npm install -g @wix/cli

      - name: Clone tests-site repo
        run: git clone https://x-access-token:${{ secrets.PS_DEV_TEAM_GITHUB_TOKEN }}@github.com/psdevteamenterprise/tests-site.git

      - name: Login to Wix (inside tests-site)
        working-directory: ./tests-site
        run: wix login --api-key ${{ secrets.PS_WIX_ACCOUNT_TOKEN }}

      - name: Install tests-site dependencies
        working-directory: ./tests-site
        run: npm install

      - name: Run integration tests (outside tests-site)
        env:
          TESTS_SITE_PATH: ./tests-site
        run: node script.js
