name: AI Lead Engineer App Test

on:
  workflow_dispatch:

permissions:
  contents: read

concurrency:
  group: ai-lead-engineer-app-${{ github.ref_name }}
  cancel-in-progress: true

jobs:
  ai-lead-engineer-app-test:
    runs-on: ubuntu-latest
    timeout-minutes: 15
    env:
      TF_CPP_MIN_LOG_LEVEL: "2"
      AIMODELSHARE_SUPPRESS_OPTIONAL_WARNINGS: "1"
      CUDA_VISIBLE_DEVICES: "-1"                # Force CPU-only for TensorFlow
      # Safely expose secrets as env (empty if unavailable, e.g., fork PRs)
      AIMODELSHARE_USERNAME: ${{ secrets.AIMODELSHARE_USERNAME }}
      AIMODELSHARE_PASSWORD: ${{ secrets.AIMODELSHARE_PASSWORD }}
      AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
      AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
      AWS_REGION: ${{ secrets.AWS_REGION }}

    strategy:
      matrix:
        python-version: ["3.11"]

    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Set up Python ${{ matrix.python-version }}
        uses: actions/setup-python@v5
        with:
          python-version: ${{ matrix.python-version }}
          cache: "pip"

      - name: Upgrade pip
        run: |
          python -m pip install --upgrade pip

      - name: Install dependencies
        run: |
          # Core dependencies
          pip install boto3 onnx onnxmltools onnxruntime Pympler scikeras shortuuid skl2onnx tf2onnx wget
          # Package in editable mode
          pip install -e .
          # Test and ML dependencies
          pip install pytest scikit-learn pandas numpy opencv-python-headless pydot regex psutil IPython matplotlib seaborn importlib_resources onnxscript
          # TensorFlow (CPU-only)
          pip install tensorflow-cpu
          # PyTorch (CPU-only)
          pip install --index-url https://download.pytorch.org/whl/cpu torch torchvision
          # Gradio UI
          pip install gradio

      - name: Verify CPU-only TensorFlow
        run: |
          python -c "import tensorflow as tf, os; os.environ['CUDA_VISIBLE_DEVICES']='-1'; print('TensorFlow GPUs:', len(tf.config.list_physical_devices('GPU'))); print('CPU-only mode verified');"

      - name: Set up test environment (if credentials available)
        if: ${{ env.AIMODELSHARE_USERNAME != '' && env.AIMODELSHARE_PASSWORD != '' }}
        run: |
          echo "username=${AIMODELSHARE_USERNAME}" >> "$GITHUB_ENV"
          echo "password=${AIMODELSHARE_PASSWORD}" >> "$GITHUB_ENV"
          echo "AWS_ACCESS_KEY_ID=${AWS_ACCESS_KEY_ID}" >> "$GITHUB_ENV"
          echo "AWS_SECRET_ACCESS_KEY=${AWS_SECRET_ACCESS_KEY}" >> "$GITHUB_ENV"
          echo "AWS_REGION=${AWS_REGION}" >> "$GITHUB_ENV"

      - name: Run AI Lead Engineer app tests
        run: |
          pytest tests/test_playground_ai_lead_engineer_app.py -v -s --tb=short

      - name: Test app import from moral_compass module
        run: |
          python - <<'PY'
          from aimodelshare.moral_compass import create_ai_lead_engineer_app, launch_ai_lead_engineer_app
          print('✓ Import from moral_compass module successful')

          from aimodelshare.moral_compass.apps import create_ai_lead_engineer_app as create2, launch_ai_lead_engineer_app as launch2
          print('✓ Import from apps module successful')

          app = create_ai_lead_engineer_app()
          assert app is not None
          print('✓ App instantiation successful')
          PY

      - name: Summary
        if: always()
        run: |
          echo "AI Lead Engineer App Test Summary"
          echo "=================================="
          echo "Python version: ${{ matrix.python-version }}"
          echo "Status: Test completed"
