name: Create compare reference

on:
#  push:
#    tags:
#      - 'ammr-*'
  workflow_dispatch:
    inputs:
      git_reference:
        description: 'Which Git reference to check out. Examples: "refs/tags/v0.2.1", "refs/heads/master".'
        default: refs/tags/vX.Y.Z
        required: true


concurrency: 
  group: compare-test-${{ github.ref }}
  cancel-in-progress: true


jobs:
  test:
    if: github.repository_owner == 'anybody'
    runs-on: [self-hosted, Windows, AnyBody]

    strategy:
      matrix:
        include:
          - ams_version: "7.5_Beta"

    env:
      ANYBODYCON: C:\Program Files\AnyBody Technology\AnyBody.${{ matrix.ams_version }}\AnyBodyCon.exe


    steps:
      - name: Set variables
        env:
          DEFAULT_GIT_REFERENCE: refs/heads/master
        run: |
          $GIT_REFERENCE="${{ github.event.inputs.git_reference || env.DEFAULT_GIT_REFERENCE }}"
          echo "GIT_REFERENCE=$GIT_REFERENCE" | Out-File -FilePath $Env:GITHUB_ENV -Encoding utf8 -Append
          echo "COMPARE_TEST_NAME=$($GIT_REFERENCE.replace("/","-"))" | Out-File -FilePath $Env:GITHUB_ENV -Encoding utf8 -Append


      - name: Checkout repository at ${{ env.GIT_REFERENCE }}
        uses: actions/checkout@v4
        with:
          ref: ${{ env.GIT_REFERENCE }}
          fetch-depth: 0

      - uses: prefix-dev/setup-pixi@v0.8.7
        with:
          environments: test
          cache-write: ${{ github.event_name == 'push' && github.ref_name == 'master' }}
          post-cleanup: true
          activate-environment: test

    
      - name: Store AMS Version
        run: |
          $AMS_VERSION = $(pixi run python -c "import anypytools; print(anypytools.tools.anybodycon_version('${{ env.ANYBODYCON }}'))")
          echo "AMS_VERSION=$AMS_VERSION" | Out-File -FilePath $Env:GITHUB_ENV -Encoding utf8 -Append

      - name: Run test for compare
        run: |
          cd Tests
          pytest --runslow --anytest-output=${{runner.temp}} --anytest-name=${{ env.GIT_REFERENCE }} --anybodycon="$Env:ANYBODYCON"

      - name: Compress compare data
        working-directory: ${{runner.temp}}
        run: |
          Compress-Archive -Force -Path ${{ env.GIT_REFERENCE }}/* -DestinationPath compare-data.zip

      - name: Create Upload script
        working-directory: ${{runner.temp}}
        run: |
          $script = @"
          import os
          import sys
          from azure.storage.blob import BlobServiceClient
          version = os.environ["AMS_VERSION"].partition(" (")[0].replace(" ", "")
          container_name = f"ams-{version.replace('.','-')}"
          blob_name = os.environ["AZURE_BLOB_NAME"]
          container_client = BlobServiceClient.from_connection_string(
              os.environ["AZURE_CONN_STR"]
          ).get_container_client(container_name)
          if not container_client.exists():
            container_client.create_container()
          with open(sys.argv[1], "rb") as fh:
              container_client.upload_blob(blob_name, fh, overwrite=True)
          "@ | Set-Content -Path "upload.py"

      - name: Upload to azure
        working-directory: ${{runner.temp}}
        env: 
          AZURE_CONN_STR: ${{ secrets.AZURE_CONN_STR }}
          AZURE_BLOB_NAME: ${{ env.COMPARE_TEST_NAME }}
        run: |
          echo $env:AZURE_BLOB_NAME
          echo $env:AZURE_CONTAINER_NAME

          python upload.py compare-data.zip

