name: EBSI Conformance Test v3.2

on:
  workflow_call:
    inputs:
      artifact:
        description: "Load image from this artifact (defaults to empty)"
        required: false
        type: string
      path:
        description: "Specifies the artifact path (defaults to empty)"
        required: false
        type: string
      tag:
        description: "Use this docker image tag (defaults to empty)"
        required: false
        type: string

jobs:
  EBSI-Conformance-Test-v3:
    name: Run
    runs-on: ubuntu-latest
    env:
      use-artifact: ${{ inputs.artifact != '' && inputs.tag != '' }}
    steps:
      - name: The EBSI Conformance Test v3.2 job is starting
        run: |
          echo -e "🔎 The EBSI Conformance Test v3.2 job is starting\n"

      - name: Check EBSI Conformance API Status
        run: |
          echo "Checking if the API is available..."
          RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" https://api-conformance.ebsi.eu/did-registry/v5/identifiers/did:ebsi:zf39qHTXaLrr6iy3tQhT3UZ)
          if [ "$RESPONSE" -ne 200 ]; then
            echo "EBSI Conformance API is down. Exiting workflow with success. ✅"
            exit 0  # Exit the workflow successfully
          else
            echo "EBSI Conformance API is up. Proceeding with tests."
          fi

      - name: Docker
        run: docker --version

      - name: Download artifact
        if: ${{ env.use-artifact }}
        uses: actions/download-artifact@v5
        with:
          name: ${{ inputs.artifact }}
          path: ${{ runner.temp }}

      - name: Unzip ${{ inputs.artifact }}.tar.gz
        if: ${{ env.use-artifact }}
        run: gunzip ${{ runner.temp }}/${{ inputs.artifact }}.tar.gz
      - name: Load Docker image from tarball
        if: ${{ env.use-artifact }}
        run: |
          docker load --input ${{ runner.temp }}/${{ inputs.artifact }}.tar

      - name: Check out repository code
        uses: actions/checkout@v5

      - name: Start ngrok tunnel
        run: |
          docker run --net=host -d -it -e NGROK_AUTHTOKEN=${{ secrets.NGROK_AUTH_TOKEN }} ngrok/ngrok:latest http 7002           
          sleep 2s

      - name: Update config files and start Issuer API
        run: |
          NGROK_URL=$(curl --retry 5 --retry-delay 5 --retry-connrefused -s http://127.0.0.1:4040/api/tunnels | jq -r '.tunnels[0].public_url')
          
          if [[ -z "$NGROK_URL" ]]; then
            echo "Error: Failed to fetch ngrok URL. Exiting..."
          exit 1
          fi

          echo "The ngork url is ${NGROK_URL}\n"       

          sed -i 's|readonly ISSUER_URL=".*"|readonly ISSUER_URL="'"${NGROK_URL}"'"|g' .github/workflows/ebsictv3/EBSICTV3-IssueToHolder.sh
          sed -i 's|baseUrl = ".*"|baseUrl = "'"${NGROK_URL}"'"|g' waltid-services/waltid-issuer-api/config/issuer-service.conf
          
          image="${{ inputs.tag }}"
          if [[ "${{ env.use-artifact }}" != "true" ]]; then
            image="waltid/issuer-api:latest"
            docker build -t $image -f waltid-services/waltid-issuer-api/Dockerfile .
          fi
          
          docker run --net=host -d -v $PWD/waltid-services/waltid-issuer-api/config:/waltid-issuer-api/config --name waltid-issuer-api $image
          
          curl --retry 5 --retry-delay 5 --retry-connrefused http://localhost:7002/livez

          ISSUER_URL=$(curl --retry 5 --retry-delay 5 --retry-connrefused http://localhost:7002/draft11/.well-known/openid-credential-issuer | jq -r '.issuer')

          if [[ "$ISSUER_URL" == "${NGROK_URL}/draft11" ]]; then
            echo -e "\nIssuer URL is valid: ${ISSUER_URL}\n"
          else
            echo -e "\nIssuer URL is invalid (matches forbidden value): $ISSUER_URL and "${NGROK_URL}/draft11"\n"
            exit 1
          fi

      - name: Execute test scripts
        env:
          EBSI_CT_PRIVATE_KEY_PARAM: "${{ secrets.EBSI_CT_PRIVATE_KEY_PARAM }}"
        run: |
          chmod +x .github/workflows/ebsictv3/EBSICTV3-IssueToHolder.sh
          ./.github/workflows/ebsictv3/EBSICTV3-IssueToHolder.sh
