parameters:
  - name: envFile
    type: string
    default: '.env.kube.dev'
  - name: ociConfigFile
    type: string
    default: 'k8s/artefatos_k8s/oracle_config_ai'
  - name: ociKeySecureFile
    type: string
    default: 'sa-agnt-ai-atendimento-contas-dev-key'
  - name: startupTimeout
    type: string
    default: '300'
  - name: stateDbCheckTimeout
    type: string
    default: '180'

steps:
  - bash: |
      set -euo pipefail
      
      curl -LsSf https://astral.sh/uv/install.sh | sh
      echo "##vso[task.prependpath]$HOME/.local/bin"
    displayName: 'instalando uv'

  - task: DownloadSecureFile@1
    name: replayOciPemKey
    displayName: 'Download OCI PEM Key'
    inputs:
      secureFile: ${{ parameters.ociKeySecureFile }}

  - bash: |
      set -euo pipefail

      sudo yum install -y gcc-c++

      echo "10.153.35.23 agt-ai-atendimento-langfuse-dev.internal.timbrasil.com.br" | sudo tee -a /etc/hosts
      echo "10.152.97.126 pegruagntaiatenddev.pe.inference.generativeai.sa-saopaulo-1.oci.oraclecloud.com" | sudo tee -a /etc/hosts
      echo "10.151.3.100 pmidfqa.internal.timbrasil.com.br" | sudo tee -a /etc/hosts
      echo "$(IP_ADB) $(HOST_ADB_1)" | sudo tee -a /etc/hosts
      echo "$(IP_ADB) $(HOST_ADB_2)" | sudo tee -a /etc/hosts

      OCI_DIR="$(Agent.TempDirectory)/replay-oci"
      mkdir -p "$OCI_DIR"
      cp "${{ parameters.ociConfigFile }}" "$OCI_DIR/config"
      cp "$(replayOciPemKey.secureFilePath)" "$OCI_DIR/key.pem"
      chmod 600 "$OCI_DIR/key.pem"

      REPLAY_OUT="$(Build.ArtifactStagingDirectory)/replay"
      mkdir -p "$REPLAY_OUT"

      uv sync --dev

      export TIM_LLM_OCI_AUTH_FILE_LOCATION="$OCI_DIR/config"
      export TIM_RAG_OCI_AUTH_FILE_LOCATION="$OCI_DIR/config"
      export TIM_RAG_OCI_USE_OKE_WORKLOAD_IDENTITY=false
      export TIM_OCI_KEY_FILE="$OCI_DIR/key.pem"

      echo "[replay] checando acesso ao banco state Oracle"
      timeout "${{ parameters.stateDbCheckTimeout }}s" \
        uv run python -m automated_conversation_test.state_db_check \
          --env-file "${{ parameters.envFile }}" \
          --tcp-timeout 10 \
        | tee "$REPLAY_OUT/state-db-check.log"

      uv run python -m automated_conversation_test.replay_blackbox \
        --env-file "${{ parameters.envFile }}" \
        --scenarios automated_conversation_test/scenarios \
        --out "$REPLAY_OUT" \
        --judge-mode hybrid \
        --startup-timeout "${{ parameters.startupTimeout }}" \
        --timeout 180

    displayName: 'Run conversation replay natively (DEV)'
    env:
      UV_INDEX_TIMBRASIL_USERNAME: XGH
      UV_INDEX_TIMBRASIL_PASSWORD: $(System.AccessToken)
      TIM_BLOQUEIO_AUTH: $(TIM_BLOQUEIO_AUTH)
      TIM_CANCELAMENTO_AUTH: $(TIM_CANCELAMENTO_AUTH)
      TIM_COMPLETE_INVOICES_AUTH: $(TIM_COMPLETE_INVOICES_AUTH)
      TIM_CONSULTA_AUTH: $(TIM_CONSULTA_AUTH)
      TIM_CONTRATO_AUTH: $(TIM_CONTRATO_AUTH)
      TIM_PROFILE_BILL_AUTH: $(TIM_PROFILE_BILL_AUTH)
      TIM_PROTOCOL_AUTH: $(TIM_PROTOCOL_AUTH)
      TIM_SERVICE_REQUEST_STATUS_AUTH: $(TIM_SERVICE_REQUEST_STATUS_AUTH)
      TIM_LLM_GATEWAY_LANGFUSE_SECRET_KEY: $(TIM_LLM_GATEWAY_LANGFUSE_SECRET_KEY_DEV)
      TIM_TRACKING_ACTIVITIES_AUTH: $(TIM_TRACKING_ACTIVITIES_AUTH)
      TIM_SMS_AUTH: $(TIM_SMS_AUTH)
      TIM_RAG_DB_PASSWORD: $(TIM_RAG_DB_PASSWORD)

  - bash: |
      set -euo pipefail
      mkdir -p "$(Build.ArtifactStagingDirectory)/replay"
    displayName: 'Ensure replay output directory'
    condition: always()

  - task: PublishTestResults@2
    displayName: 'Publish replay test results'
    condition: always()
    inputs:
      testResultsFormat: 'JUnit'
      testResultsFiles: '**/junit.xml'
      searchFolder: '$(Build.ArtifactStagingDirectory)/replay'
      testRunTitle: 'Conversation replay DEV'
      failTaskOnFailedTests: false
      failTaskOnMissingTestResultsFile: false

  - task: PublishPipelineArtifact@1
    displayName: 'Publish replay artifacts'
    condition: always()
    inputs:
      targetPath: '$(Build.ArtifactStagingDirectory)/replay'
      artifact: 'conversation-replay-dev'
      publishLocation: 'pipeline'
