image: maven:3.6.3-openjdk-8-slim  

pipelines:
  branches:
    develop:
      - step:
          name: run unit test cases
          caches:
            - maven
            - node
          script:
            - NODE_VERSION=21
            - curl -fsSL https://deb.nodesource.com/setup_$NODE_VERSION.x | bash -  # Setup Node.js
            - apt-get install -y nodejs  # Install Node.js
            - node -v  # Verify Node.js installation
            - npm -v   # Verify npm installation
            - mvn -f plugins/pom.xml clean compile test

      - parallel:   
        - step:
              name: Plugin - ACCOUNT
              trigger: manual  # Requires manual selection before proceeding
              caches:
                - maven
                - node
              script:
                # Define Fluent account credentials
                - NODE_VERSION=21
                - FLUENT_ACCOUNT_ID="$dev_fluentAccountId"
                - FLUENT_API_HOST="$dev_fluentApiHost"
                - FLUENT_USERNAME="$dev_fluentUserName"
                - FLUENT_PASSWORD="$dev_fluentPassword"
                - FLUENT_CLIENT_SECRET="$dev_fluentClientSecret"
                - FLUENT_PROFILE="$dev_fluentAccountId"

                # Define Fluent retailer credentials
                - RETAILER_REF="$dev_retailer_<retailer_ref_in_lowercase>"
                - RETAILER_ID="$dev_retailer_<retailer_ref_in_lowercase>_id"
                - RETAILER_USERNAME="$dev_retailer_<retailer_ref_in_lowercase>_username"
                - RETAILER_PASSWORD="$dev_retailer_<retailer_ref_in_lowercase>_password"

                - apt-get update && apt-get install -y zip rsync
                - curl -fsSL https://deb.nodesource.com/setup_$NODE_VERSION.x | bash -
                - apt-get install -y nodejs
                - mvn -version  
                - node -v
                - npm -v
                
                # Install Fluent CLI
                - npm install -g https://downloads.fluentcommerce.com/v1.0.0/fluent-cli/release/latest
                
                # Verify Fluent CLI Installation
                - fluent --version
                - fluent --help

                # Create Fluent Profile
                - fluent profile create "$FLUENT_PROFILE" --id "$FLUENT_ACCOUNT_ID" --base-url "$FLUENT_API_HOST" --username "$FLUENT_USERNAME" --password "$FLUENT_PASSWORD" --client-secret "$FLUENT_CLIENT_SECRET"

                # Update Retailer Profile
                - fluent profile update "$FLUENT_PROFILE" --retailer "$RETAILER_REF" --id "$RETAILER_ID" --username "$RETAILER_USERNAME" --password "$RETAILER_PASSWORD"

                # List the JSON files created
                - echo "Listing JSON files in ~/.fluentcommerce/$FLUENT_PROFILE/"
                - ls -l ~/.fluentcommerce/"$FLUENT_PROFILE"/*.json || echo "No JSON files found"

                # Display content of profile.json if it exists
                - PROFILE_JSON_PATH=~/.fluentcommerce/"$FLUENT_PROFILE"/profile.json
                - if [ -f "$PROFILE_JSON_PATH" ]; then
                    echo "Contents of profile.json:";
                    cat "$PROFILE_JSON_PATH";
                  else
                    echo "profile.json not found.";
                  fi

                # Display content of retailer JSON if it exists
                - RETAILER_JSON_PATH=~/.fluentcommerce/"$FLUENT_PROFILE"/retailer."$RETAILER_REF".json
                - if [ -f "$RETAILER_JSON_PATH" ]; then
                    echo "Contents of retailer JSON:";
                    cat "$RETAILER_JSON_PATH";
                  else
                    echo "retailer JSON not found.";
                  fi

                # Grant execution permission and run build-module.sh
                - chmod +x scripts/build-module.sh  
                - ./scripts/build-module.sh  

                # Ensure the dist directory exists and list its contents
                - echo "Checking dist/ directory contents:"
                - ls -l dist/ || echo "dist/ directory not found."

                # Run Fluent Module Describe
                - fluent module describe dist/

                # Extract the zip filename from the dist/ directory
                - MODULE_ZIP=$(find dist/ -maxdepth 1 -type f -name "*.zip" | head -n 1 | xargs -n 1 basename)

                # Ensure a zip file was found before proceeding
                - |
                  if [ -z "$MODULE_ZIP" ]; then
                    echo "Error: No module zip file found in dist/ directory.";
                    exit 1;
                  fi
                  echo "Found module zip file: $MODULE_ZIP"

                # Run the config command
                - fluent module config "dist/$MODULE_ZIP" --retailer "$RETAILER_REF" --profile "$FLUENT_PROFILE"

                # Find the module.config JSON file
                - CONFIG_JSON=$(ls module.config*.json | head -n 1)

                # Ensure a JSON file was found before proceeding
                - |
                  if [ -z "$CONFIG_JSON" ]; then
                    echo "Error: No module.config JSON file found.";
                    exit 1;
                  fi

                # Install the Fluent module using the extracted zip file and JSON config
                - fluent module install "dist/$MODULE_ZIP" --config "$CONFIG_JSON" --include rules  --retailer "$RETAILER_REF" --profile "$FLUENT_PROFILE"
  
        - step:
            name: Data - <retailer_ref>
            trigger: manual  # Requires manual selection before proceeding
            caches:
              - maven
              - node
            script:
              # Define Fluent account credentials
              - NODE_VERSION=21
              - FLUENT_ACCOUNT_ID="$dev_fluentAccountId"
              - FLUENT_API_HOST="$dev_fluentApiHost"
              - FLUENT_USERNAME="$dev_fluentUserName"
              - FLUENT_PASSWORD="$dev_fluentPassword"
              - FLUENT_CLIENT_SECRET="$dev_fluentClientSecret"
              - FLUENT_PROFILE="$dev_fluentAccountId"

              # Define Fluent retailer credentials
              - RETAILER_REF="$dev_retailer_<retailer_ref_in_lowercase>"
              - RETAILER_ID="$dev_retailer_<<retailer_ref_in_lowercase>>_id"
              - RETAILER_USERNAME="$dev_retailer_<<retailer_ref_in_lowercase>>_username"
              - RETAILER_PASSWORD="$dev_retailer_<<retailer_ref_in_lowercase>>_password"

              - apt-get update && apt-get install -y zip rsync
              - curl -fsSL https://deb.nodesource.com/setup_$NODE_VERSION.x | bash -
              - apt-get install -y nodejs
              - mvn -version  
              - node -v
              - npm -v
              
              # Install Fluent CLI
              - npm install -g https://downloads.fluentcommerce.com/v1.0.0/fluent-cli/release/latest
              
              # Verify Fluent CLI Installation
              - fluent --version
              - fluent --help

              # Create Fluent Profile
              - fluent profile create "$FLUENT_PROFILE" --id "$FLUENT_ACCOUNT_ID" --base-url "$FLUENT_API_HOST" --username "$FLUENT_USERNAME" --password "$FLUENT_PASSWORD" --client-secret "$FLUENT_CLIENT_SECRET"

              # Update Retailer Profile
              - fluent profile update "$FLUENT_PROFILE" --retailer "$RETAILER_REF" --id "$RETAILER_ID" --username "$RETAILER_USERNAME" --password "$RETAILER_PASSWORD"

              # List the JSON files created
              - echo "Listing JSON files in ~/.fluentcommerce/$FLUENT_PROFILE/"
              - ls -l ~/.fluentcommerce/"$FLUENT_PROFILE"/*.json || echo "No JSON files found"

              # Display content of profile.json if it exists
              - PROFILE_JSON_PATH=~/.fluentcommerce/"$FLUENT_PROFILE"/profile.json
              - if [ -f "$PROFILE_JSON_PATH" ]; then
                  echo "Contents of profile.json:";
                  cat "$PROFILE_JSON_PATH";
                else
                  echo "profile.json not found.";
                fi

              # Display content of retailer JSON if it exists
              - RETAILER_JSON_PATH=~/.fluentcommerce/"$FLUENT_PROFILE"/retailer."$RETAILER_REF".json
              - if [ -f "$RETAILER_JSON_PATH" ]; then
                  echo "Contents of retailer JSON:";
                  cat "$RETAILER_JSON_PATH";
                else
                  echo "retailer JSON not found.";
                fi

              # Grant execution permission and run build-module.sh
              - chmod +x scripts/build-module.sh  
              - ./scripts/build-module.sh  

              # Ensure the dist directory exists and list its contents
              - echo "Checking dist/ directory contents:"
              - ls -l dist/ || echo "dist/ directory not found."

              # Run Fluent Module Describe
              - fluent module describe dist/

              # Extract the zip filename from the dist/ directory
              - MODULE_ZIP=$(find dist/ -maxdepth 1 -type f -name "*.zip" | head -n 1 | xargs -n 1 basename)

              # Ensure a zip file was found before proceeding
              - |
                if [ -z "$MODULE_ZIP" ]; then
                  echo "Error: No module zip file found in dist/ directory.";
                  exit 1;
                fi
                echo "Found module zip file: $MODULE_ZIP"

              # Run the config command
              - fluent module config "dist/$MODULE_ZIP" --retailer "$RETAILER_REF" --profile "$FLUENT_PROFILE"

              # Find the module.config JSON file
              - CONFIG_JSON=$(ls module.config*.json | head -n 1)

              # Ensure a JSON file was found before proceeding
              - |
                if [ -z "$CONFIG_JSON" ]; then
                  echo "Error: No module.config JSON file found.";
                  exit 1;
                fi

              # Install the Fluent module using the extracted zip file and JSON config
              - fluent module install "dist/$MODULE_ZIP" --config "$CONFIG_JSON" --exclude rules --retailer "$RETAILER_REF" --profile "$FLUENT_PROFILE"        
    
    
        - step:
            name: Data - <retailer_ref>
            trigger: manual  # Requires manual selection before proceeding
            caches:
              - maven
              - node
            script:
              # Define Fluent account credentials
              - NODE_VERSION=21
              - FLUENT_ACCOUNT_ID="$dev_fluentAccountId"
              - FLUENT_API_HOST="$dev_fluentApiHost"
              - FLUENT_USERNAME="$dev_fluentUserName"
              - FLUENT_PASSWORD="$dev_fluentPassword"
              - FLUENT_CLIENT_SECRET="$dev_fluentClientSecret"
              - FLUENT_PROFILE="$dev_fluentAccountId"

              # Define Fluent retailer credentials
              - RETAILER_REF="$dev_retailer_<retailer_ref_in_lowercase>"
              - RETAILER_ID="$dev_retailer_<retailer_ref_in_lowercase>_id"
              - RETAILER_USERNAME="$dev_retailer_<retailer_ref_in_lowercase>_username"
              - RETAILER_PASSWORD="$dev_retailer_<retailer_ref_in_lowercase>_password"

              - apt-get update && apt-get install -y zip rsync
              - curl -fsSL https://deb.nodesource.com/setup_$NODE_VERSION.x | bash -
              - apt-get install -y nodejs
              - mvn -version  
              - node -v
              - npm -v
              
              # Install Fluent CLI
              - npm install -g https://downloads.fluentcommerce.com/v1.0.0/fluent-cli/release/latest
              
              # Verify Fluent CLI Installation
              - fluent --version
              - fluent --help

              # Create Fluent Profile
              - fluent profile create "$FLUENT_PROFILE" --id "$FLUENT_ACCOUNT_ID" --base-url "$FLUENT_API_HOST" --username "$FLUENT_USERNAME" --password "$FLUENT_PASSWORD" --client-secret "$FLUENT_CLIENT_SECRET"

              # Update Retailer Profile
              - fluent profile update "$FLUENT_PROFILE" --retailer "$RETAILER_REF" --id "$RETAILER_ID" --username "$RETAILER_USERNAME" --password "$RETAILER_PASSWORD"

              # List the JSON files created
              - echo "Listing JSON files in ~/.fluentcommerce/$FLUENT_PROFILE/"
              - ls -l ~/.fluentcommerce/"$FLUENT_PROFILE"/*.json || echo "No JSON files found"

              # Display content of profile.json if it exists
              - PROFILE_JSON_PATH=~/.fluentcommerce/"$FLUENT_PROFILE"/profile.json
              - if [ -f "$PROFILE_JSON_PATH" ]; then
                  echo "Contents of profile.json:";
                  cat "$PROFILE_JSON_PATH";
                else
                  echo "profile.json not found.";
                fi

              # Display content of retailer JSON if it exists
              - RETAILER_JSON_PATH=~/.fluentcommerce/"$FLUENT_PROFILE"/retailer."$RETAILER_REF".json
              - if [ -f "$RETAILER_JSON_PATH" ]; then
                  echo "Contents of retailer JSON:";
                  cat "$RETAILER_JSON_PATH";
                else
                  echo "retailer JSON not found.";
                fi

              # Grant execution permission and run build-module.sh
              - chmod +x scripts/build-module.sh  
              - ./scripts/build-module.sh  

              # Ensure the dist directory exists and list its contents
              - echo "Checking dist/ directory contents:"
              - ls -l dist/ || echo "dist/ directory not found."

              # Run Fluent Module Describe
              - fluent module describe dist/

              # Extract the zip filename from the dist/ directory
              - MODULE_ZIP=$(find dist/ -maxdepth 1 -type f -name "*.zip" | head -n 1 | xargs -n 1 basename)

              # Ensure a zip file was found before proceeding
              - |
                if [ -z "$MODULE_ZIP" ]; then
                  echo "Error: No module zip file found in dist/ directory.";
                  exit 1;
                fi
                echo "Found module zip file: $MODULE_ZIP"

              # Run the config command
              - fluent module config "dist/$MODULE_ZIP" --retailer "$RETAILER_REF" --profile "$FLUENT_PROFILE"

              # Find the module.config JSON file
              - CONFIG_JSON=$(ls module.config*.json | head -n 1)

              # Ensure a JSON file was found before proceeding
              - |
                if [ -z "$CONFIG_JSON" ]; then
                  echo "Error: No module.config JSON file found.";
                  exit 1;
                fi

              # Install the Fluent module using the extracted zip file and JSON config
              - fluent module install "dist/$MODULE_ZIP" --config "$CONFIG_JSON" --exclude rules  --retailer "$RETAILER_REF" --profile "$FLUENT_PROFILE"       
              
        - step:
            name: e2e - <retailer_ref_in_lowercase>
            trigger: manual  # Requires manual selection before proceeding
            caches:
              - maven
              - node
            script:
              # Define Fluent account credentials
              - NODE_VERSION=21
              - export FLUENT_ACCOUNT_ID="$dev_fluentAccountId"
              - export FLUENT_USERNAME="$dev_fluentUserName"
              - export FLUENT_PASSWORD="$dev_fluentPassword"
              - export FLUENT_CLIENT_SECRET="$dev_fluentClientSecret_integration"

              # Define Fluent retailer credentials
              - export RETAILER_REF="$dev_retailer_<retailer_ref_in_lowercase>"
              - export RETAILER_USERNAME="$dev_retailer_<retailer_ref_in_lowercase>_username"
              - export RETAILER_PASSWORD="$dev_retailer_<retailer_ref_in_lowercase>_password"

              - apt-get update && apt-get install -y zip rsync
              - curl -fsSL https://deb.nodesource.com/setup_$NODE_VERSION.x | bash -
              - apt-get install -y nodejs
              - mvn -version  
              - node -v
              - npm -v

              - mvn -f plugins/e2e/pom.xml test -Dtest=CreateOrderTest#createOrderAndCancelOrder 
        
        - step:
            name: e2e - <retailer_ref>
            trigger: manual  # Requires manual selection before proceeding
            caches:
              - maven
              - node
            script:
              - echo "deploy e2e for next Retailer "
