name: Moral Compass Rank Integration Test

on:
  workflow_dispatch:
    inputs:
      sessionid:
        description: 'Session ID for test user (from login URL)'
        required: false
        type: string
      table_id:
        description: 'Optional Moral Compass table ID override'
        required: false
        type: string
      debug_log:
        description: 'Enable debug logging'
        required: false
        type: boolean
        default: false

permissions:
  contents: read

concurrency:
  group: mc-rank-integration-${{ github.ref }}
  cancel-in-progress: true

jobs:
  rank-integration-test:
    runs-on: ubuntu-latest
    timeout-minutes: 20
    env:
      TF_CPP_MIN_LOG_LEVEL: "2"
      AIMODELSHARE_SUPPRESS_OPTIONAL_WARNINGS: "1"
    
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
      
      - name: Set up Python 3.11
        uses: actions/setup-python@v5
        with:
          python-version: "3.11"
      
      - name: Install system deps
        run: |
          sudo apt-get update
          sudo apt-get install -y libatlas-base-dev gfortran
          sudo apt-get install -y --no-install-recommends \
            libgl1 \
            libglib2.0-0 \
            gcc \
            python3-dev
          sudo rm -rf /var/lib/apt/lists/*

      - name: Install Python dependencies
        run: |
          python -m pip install --upgrade pip
          pip install --no-cache-dir -r requirements-apps.txt
          pip install --no-cache-dir aimodelshare --no-dependencies
          pip install --no-cache-dir pytest
      
      - name: Mask sensitive inputs
        run: |
          # Mask sessionid if provided via input
          if [ -n "${{ inputs.sessionid }}" ]; then
            echo "::add-mask::${{ inputs.sessionid }}"
          fi
          # Mask sessionid if provided via secret
          if [ -n "${{ secrets.TEST_SESSION_ID }}" ]; then
            echo "::add-mask::${{ secrets.TEST_SESSION_ID }}"
          fi
      
      - name: Set up test environment
        env:
          INPUT_SESSION_ID: ${{ inputs.sessionid }}
          SECRET_SESSION_ID: ${{ secrets.TEST_SESSION_ID }}
          TABLE_ID: ${{ inputs.table_id }}
          DEBUG_LOG: ${{ inputs.debug_log }}
          MORAL_COMPASS_API_BASE_URL: ${{ secrets.MORAL_COMPASS_API_BASE_URL }}
          PLAYGROUND_URL: ${{ secrets.PLAYGROUND_URL }}
        run: |
          # Use input sessionid if provided, otherwise use secret
          if [ -n "$INPUT_SESSION_ID" ]; then
            echo "SESSION_ID=$INPUT_SESSION_ID" >> $GITHUB_ENV
          elif [ -n "$SECRET_SESSION_ID" ]; then
            echo "SESSION_ID=$SECRET_SESSION_ID" >> $GITHUB_ENV
          else
            echo "SESSION_ID=" >> $GITHUB_ENV
          fi
          
          # Set optional table_id
          if [ -n "$TABLE_ID" ]; then
            echo "TABLE_ID=$TABLE_ID" >> $GITHUB_ENV
          fi
          
          # Set debug log flag
          if [ "$DEBUG_LOG" = "true" ]; then
            echo "DEBUG_LOG=true" >> $GITHUB_ENV
          else
            echo "DEBUG_LOG=false" >> $GITHUB_ENV
          fi
          
          # Set API URLs
          if [ -n "$MORAL_COMPASS_API_BASE_URL" ]; then
            echo "MORAL_COMPASS_API_BASE_URL=$MORAL_COMPASS_API_BASE_URL" >> $GITHUB_ENV
          fi
          
          if [ -n "$PLAYGROUND_URL" ]; then
            echo "PLAYGROUND_URL=$PLAYGROUND_URL" >> $GITHUB_ENV
          fi
      
      - name: Run Moral Compass rank integration test
        run: |
          python scripts/mc_rank_integration_test.py
      
      - name: Upload test logs (on failure)
        if: failure()
        uses: actions/upload-artifact@v4
        with:
          name: test-logs
          path: /tmp/mc_rank_test_*.log
          retention-days: 7
