on:
  workflow_call:
    inputs:
      distrib:
        required: true
        type: string
      arch:
        required: true
        type: string
      image:
        required: true
        type: string
      image_test:
        required: true
        type: string
      image_version:
        required: true
        type: string
      package_cache_key:
        required: true
        type: string
      package_cache_path:
        required: true
        type: string
      database_type:
        required: true
        type: string
      tests_params:
        required: true
        type: string
      test_group_name:
        required: true
        type: string
    secrets:
      registry_username:
        required: true
      registry_password:
        required: true
      collect_s3_access_key:
        required: true
      collect_s3_secret_key:
        required: true
      xray_client_id:
        required: true
      xray_client_secret:
        required: true

jobs:
  test-image-to-cache:
    runs-on: ${{ contains(inputs.image, 'arm') && fromJson('["self-hosted", "collect-arm64"]') || 'ubuntu-24.04' }}
    steps:
      - name: Checkout sources
        uses: actions/checkout@d632683dd7b4114ad314bca15554477dd762a938 # v4.2.0

      - name: Login to Registry
        uses: docker/login-action@9780b0c442fbb1117ed29e0efdff1e18412f7567 # v3.3.0
        with:
          registry: ${{ vars.DOCKER_INTERNAL_REGISTRY_URL }}
          username: ${{ secrets.registry_username }}
          password: ${{ secrets.registry_password }}

      - name: Login to Proxy Registry
        uses: docker/login-action@9780b0c442fbb1117ed29e0efdff1e18412f7567 # v3.3.0
        with:
          registry: ${{ vars.DOCKER_PROXY_REGISTRY_URL }}
          username: ${{ secrets.registry_username }}
          password: ${{ secrets.registry_password }}

      - name: load image
        run: |
          docker pull ${{ vars.DOCKER_INTERNAL_REGISTRY_URL }}/${{inputs.image_test}}
        shell: bash

      - name: image to disk
        run: |
          docker save -o /tmp/${{inputs.image}} ${{ vars.DOCKER_INTERNAL_REGISTRY_URL }}/${{inputs.image_test}}
        shell: bash

      - name: image to cache
        uses: actions/cache/save@0c45773b623bea8c8e75f6c82b208c3cf94ea4f9 # v4.0.2
        with:
          path: /tmp/${{inputs.image}}
          key: ${{inputs.image_test}}

  robot-test-list:
    needs: [test-image-to-cache]
    runs-on: ubuntu-24.04
    outputs:
      features: ${{ steps.list-features.outputs.features }}

    steps:
      - name: Checkout sources
        uses: actions/checkout@d632683dd7b4114ad314bca15554477dd762a938 # v4.2.0

      - name: List features
        id: list-features
        run: |
          cd tests
          echo "features=$(grep -Rl '*** Test Cases ***' . | grep '.robot' | sed -e "s#^./##" | sort | jq -R -s -c 'split("\n")[:-1]')" >> $GITHUB_OUTPUT

  robot-test:
    needs: [robot-test-list]
    runs-on: ${{ contains(inputs.image, 'arm') && fromJson('["self-hosted", "collect-arm64"]') || 'ubuntu-24.04' }}

    strategy:
      fail-fast: false
      matrix:
        feature: ${{ fromJson(needs.robot-test-list.outputs.features) }}

    name: ${{ matrix.feature }} ${{inputs.test_group_name}}

    steps:
      - name: Checkout sources
        uses: actions/checkout@d632683dd7b4114ad314bca15554477dd762a938 # v4.2.0
        with:
          fetch-depth: 0

      - name: Restore image
        uses: actions/cache/restore@0c45773b623bea8c8e75f6c82b208c3cf94ea4f9 # v4.0.2
        with:
          path: /tmp/${{inputs.image}}
          key: ${{inputs.image_test}}
          fail-on-cache-miss: true

      - name: Restore packages
        uses: actions/cache/restore@0c45773b623bea8c8e75f6c82b208c3cf94ea4f9 # v4.0.2
        with:
          path: ${{ inputs.package_cache_path }}
          key: ${{ inputs.package_cache_key }}
          fail-on-cache-miss: true

      - name: load image
        run: |
          docker load --input /tmp/${{ inputs.image }}

      - name: Test ${{ matrix.feature }}
        env:
          AWS_ACCESS_KEY_ID: ${{ secrets.collect_s3_access_key }}
          AWS_SECRET_ACCESS_KEY: ${{ secrets.collect_s3_secret_key }}
        run: |
          docker run --rm --privileged --ulimit core=-1 --security-opt seccomp=unconfined \
            -v $(pwd):/test_collect  \
            --env AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID \
            --env AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY \
            --env AWS_BUCKET=centreon-collect-robot-report \
            --workdir /test_collect \
            ${{ vars.DOCKER_INTERNAL_REGISTRY_URL }}/${{inputs.image_test}} \
            ./.github/scripts/collect-test-robot.sh \
            ${{ matrix.feature }} ${{inputs.database_type}} ${{inputs.tests_params}}

      # - name: Generate Xray Token
      #   id: generate-xray-token
      #   run: |
      #     token_response=$(curl -H "Content-Type: application/json" -X POST --data "{\"client_id\": \"${{ secrets.XRAY_CLIENT_ID }}\", \"client_secret\": \"${{ secrets.XRAY_CLIENT_SECRET }}\"}" "https://xray.cloud.getxray.app/api/v1/authenticate")
      #     xray_token=$(echo "$token_response" | sed -n 's/.*"\(.*\)".*/\1/p')
      #     echo "xray_token=$xray_token" >> $GITHUB_OUTPUT
      #   shell: bash

      # - name: Send report to xrays
      #   run: |
      #     curl -H "Content-Type: multipart/form-data" -X POST -F info=@tests/issueFields.json -F results=@tests/output.xml -F testInfo=@tests/testIssueFields.json -H "Authorization: Bearer ${{ steps.generate-xray-token.outputs.xray_token }}" https://xray.cloud.getxray.app/api/v2/import/execution/robot/multipart

      - name: Move reports
        if: ${{ failure() }}
        run: |
          mkdir reports
          FILE_PREFIX=`echo "${{ matrix.feature }}" | sed -e "s#/#__#g"`-${{inputs.test_group_name}}
          if [ -d tests/failed ] ; then
            cp -rp tests/failed reports/$FILE_PREFIX-failed
            cp tests/log.html reports/$FILE_PREFIX-log.html
            cp tests/output.xml reports/$FILE_PREFIX-output.xml
          fi

      - name: Replace / with - in the feature path
        id: feature-path
        if: always()
        run: |
          feature_name="${{ matrix.feature }}"
          feature_name_with_dash="${feature_name//\//-}"
          echo "Modified Feature Name: $feature_name_with_dash"
          echo "feature_name_with_dash=$feature_name_with_dash" >> $GITHUB_OUTPUT

      - name: Upload Test Results
        if: ${{ failure() }}
        uses: actions/upload-artifact@50769540e7f4bd5e21e526ee35c689e35e0d6874 # v4.4.0
        with:
          name: reports-${{inputs.test_group_name}}-${{ steps.feature-path.outputs.feature_name_with_dash }}
          path: reports
          retention-days: 1

  robot-test-report:
    needs: [robot-test]
    if: ${{ failure() }}
    runs-on: ubuntu-24.04

    steps:
      - uses: actions/checkout@d632683dd7b4114ad314bca15554477dd762a938 # v4.2.0

      - name: Download Artifacts
        uses: actions/download-artifact@fa0a91b85d4f404e444e00e005971372dc801d16 # v4.1.8
        with:
          pattern: reports-${{inputs.test_group_name}}-*
          path: reports
          merge-multiple: true

      - name: Upload the regrouped artifact
        uses: actions/upload-artifact@50769540e7f4bd5e21e526ee35c689e35e0d6874 # v4.4.0
        with:
          name: reports-${{inputs.test_group_name}}
          path: reports/
          retention-days: 1

      - name: Delete separated artifacts
        run: |
          artifact_pattern="reports-${{inputs.test_group_name}}-"
          TOKEN=${{ secrets.GITHUB_TOKEN }}
          artifact_exist=true
          while [ "$artifact_exist" = true ] ;do
            artifact_exist=false
            artifacts_response=$(curl -L \
                          -H "Accept: application/vnd.github+json" \
                          -H "Authorization: Bearer $TOKEN" \
                          -H "X-GitHub-Api-Version: 2022-11-28" \
                          "https://api.github.com/repos/${{ github.repository }}/actions/artifacts?per_page=100")
            artifacts=$(echo $artifacts_response | jq -c '.artifacts[]')
            echo "Those are the artifacts : $artifacts"
            while read row; do
              artifact_name=$(echo "$row" | jq -r '.name')
              if [[ "$artifact_name" =~ ^.*"$artifact_pattern".* ]]; then
                artifact_exist=true
                echo "Deleting : $artifact_name"
                artifact_id=$(echo "$row" | jq -r '.id')
                curl -L \
                  -X DELETE \
                  -H "Accept: application/vnd.github+json" \
                  -H "Authorization: Bearer $TOKEN" \
                  -H "X-GitHub-Api-Version: 2022-11-28" \
                  "https://api.github.com/repos/${{ github.repository }}/actions/artifacts/${artifact_id}"
              fi
            done <<< "$artifacts"
          done
          echo "End of Deleting"
        shell: bash

        # setup-python v5.0.0 relies on node20 which is not supported by el7 distributions
      - uses: actions/setup-python@f677139bbe7f9c59b41e40162b753c062f5d49a3 # v5.2.0
        if: ${{ inputs.distrib  == 'el7'}}
        with:
          python-version: "3.10"

      - uses: actions/setup-python@f677139bbe7f9c59b41e40162b753c062f5d49a3 # v5.2.0
        if: ${{ inputs.distrib != 'el7' }}
        with:
          python-version: "3.10"

      - run: |
          pip3 install -U robotframework robotframework-databaselibrary pymysql python-dateutil
          cd reports
          rebot -o output.xml ./*-output.xml || true
        shell: bash

      - name: Send report to commit
        uses: joonvena/robotframework-reporter-action@229b6d4248b20be6e54f4fc32c7414130d1bf200 # v2.5
        with:
          gh_access_token: ${{ secrets.GITHUB_TOKEN }}
          report_path: reports
          show_passed_tests: false
