name: Concrete ML Tests
on:
  workflow_dispatch:
    inputs:
      instance_id:
        description: 'Instance ID'
        type: string
      instance_image_id:
        description: 'Instance AMI ID'
        type: string
      instance_type:
        description: 'Instance product type'
        type: string
      runner_name:
        description: 'Action runner name'
        type: string
      request_id:
        description: 'Slab request ID'
        type: string


env:
  DOCKER_IMAGE: ghcr.io/zama-ai/concrete-compiler

jobs:
  linux-x86:
    strategy:
      matrix:
        python-version: ["3.10"]

    runs-on: ${{ github.event.inputs.runner_name }}
    steps:
      - name: Log instance configuration
        run: |
          echo "IDs: ${{ inputs.instance_id }}"
          echo "AMI: ${{ inputs.instance_image_id }}"
          echo "Type: ${{ inputs.instance_type }}"
          echo "Request ID: ${{ inputs.request_id }}"
          echo "User Inputs: ${{ inputs.user_inputs }}"

      - name: Set up SSH agent
        uses: webfactory/ssh-agent@v0.7.0
        with:
          ssh-private-key: ${{ secrets.CONCRETE_CI_SSH_PRIVATE }}

      - name: Set up GitHub environment
        run: |
          echo "HOME=/home/ubuntu" >> "${GITHUB_ENV}"
          #echo "SSH_AUTH_SOCK=$SSH_AUTH_SOCK)" >> "${GITHUB_ENV}"
          echo "SSH_AUTH_SOCK_DIR=$(dirname $SSH_AUTH_SOCK)" >> "${GITHUB_ENV}"

      - name: Checkout
        uses: actions/checkout@v3
        with:
          submodules: recursive
          token: ${{ secrets.CONCRETE_ACTIONS_TOKEN }}

      - name: Set release version
        run: echo "__version__ = \"$(date +"%Y.%m.%d")\"" >| frontends/concrete-python/version.txt

      - name: Expose release version from Python
        run: cp frontends/concrete-python/version.txt frontends/concrete-python/concrete/fhe/version.py

      - name: Create build directory
        run: mkdir build

      - name: Build wheel
        uses: addnab/docker-run-action@v3
        id: build-compiler-bindings
        with:
          registry: ghcr.io
          image: ${{ env.DOCKER_IMAGE }}
          username: ${{ secrets.GHCR_LOGIN }}
          password: ${{ secrets.GHCR_PASSWORD }}
          options: >-
            -v ${{ github.workspace }}:/concrete
            -v ${{ github.workspace }}/build:/build
            -v ${{ env.SSH_AUTH_SOCK }}:/ssh.socket
            -e SSH_AUTH_SOCK=/ssh.socket
            ${{ env.DOCKER_GPU_OPTION }}
          shell: bash
          run: |
            set -e
            rm -rf /build/*
            
            export PYTHON=${{ format('python{0}', matrix.python-version) }}
            echo "Using $PYTHON"

            cd /concrete/frontends/concrete-python
            make PYTHON=$PYTHON venv
            source .venv/bin/activate
            
            cd /concrete/compilers/concrete-compiler/compiler
            make BUILD_DIR=/build CCACHE=ON DATAFLOW_EXECUTION_ENABLED=ON Python3_EXECUTABLE=$(which python) python-bindings
            
            echo "Debug: ccache statistics (after the build):"
            ccache -s

            cd /concrete/frontends/concrete-python

            export COMPILER_BUILD_DIRECTORY="/build"
            make whl
            
            deactivate

      - name: Setup Python
        uses: actions/setup-python@v4
        with:
          python-version: ${{ matrix.python-version }}

      - name: ML Tests
        run: |
          export CONCRETE_PYTHON_WHEEL=$(pwd)/frontends/concrete-python/dist/*manylinux*.whl
          apt update
          apt install git git-lfs -y
          pip install poetry==1.2.2
          ./ci/scripts/test_cml.sh --use-wheel $CONCRETE_PYTHON_WHEEL --verbose
