name: Django CI Pipeline

on:
  push:
    branches: 
      - main
      - release
      - testing-pipeline
  pull_request:
    branches:
      - main
      - release

jobs:
  test:

    runs-on: self-hosted

    env:
      SECRET_KEY: "t3st-dummy-s3cr3t-k3y"
      STATIC_ROOT: "tmp/static/"
      MEDIA_ROOT: "tmp/media/"
      DOMAIN: "testserver"

    steps:
    - uses: actions/checkout@v4

    - name: Check version and distribution
      run: |
        uname -srm
        cat /etc/os-release

    - name: Install python3-venv
      id: install
      run: |
        apt-get update
        apt-get install python3 python3-venv python3-pip -y

    - name: Check Python version
      run: |
        python3 --version

    - name: Create virtual environment
      run: |
        python3 -m venv venv
        source venv/bin/activate
        echo "Virtual environment created successfully"
      # https://docs.github.com/en/actions/learn-github-actions/contexts#steps-context
      if: steps.install.outcome == 'success'

    - name: Get DIDKit wheel
      id: didkit
      run: |
        wget -O didkit-0.3.2-cp311-cp311-manylinux_2_34_x86_64.whl https://gitea.pangea.org/api/v1/repos/trustchain-oc1-orchestral/ssikit_trustchain/raw/didkit-0.3.2-cp311-cp311-manylinux_2_34_x86_64.whl?token=${{ secrets.FILE_GETTER_TOKEN }}
        echo "Successfully downloaded DIDkit"

    - name: Install dependencies
      run: |
        source venv/bin/activate
        pip install --upgrade pip
        pip install -r requirements.txt
      if: steps.didkit.outcome == 'success'

    - name: Run tests
      run: |
        source venv/bin/activate
        coverage run manage.py test
        # python manage.py test
        #
    - name: Coverage report
      run: |
        source venv/bin/activate
        coverage report

  ssikit-tests:
    needs: test
    runs-on: self-hosted
    steps:

      - name: Checkout SSIKit repo
        uses: actions/checkout@v4
        with:
          repository: trustchain-oc1-orchestral/ssikit_trustchain
          submodules: true
          ref: master
          # might not be needed
          token: ${{ secrets.SSIKIT_TOKEN }}

      - name: Install python3-venv (if not already installed)
        run: |
          apt-get update
          apt-get install python3 python3-venv python3-pip -y

      - name: Setup Python environment
        run: |
          python3 -m venv venv
          source venv/bin/activate

      - name: Install dependencies
        run: |
          source venv/bin/activate
          pip install --upgrade pip
          # Install any additional dependencies required for SSIKit tests here
          pip install -r requirements.txt

      - name: Execute SSIKit tests
        run: |
          source venv/bin/activate
          python main.py

  deploy-testing-instances:
    needs: test
    runs-on: self-hosted
    steps:
      - uses: actions/checkout@v4

      - name: Trigger Remote Script
        run: |
          response=$(curl -s -o /dev/null -w "%{http_code}" -X POST http://45.150.187.54:5000/trigger-script -H "Authorization: SecretToken")
          if [ "$response" -ne 200 ]; then
          echo "Script execution failed with HTTP status $response"
          exit 1
          else 
          echo "Script execution successful"
          exit 0
          fi
        if: success() && github.ref == 'refs/heads/release'

  end2end-tests:
    needs: deploy-testing-instances
    runs-on: self-hosted
    steps:

      - name: Checkout E2E tests repo
        uses: actions/checkout@v4
        with:
          repository: trustchain-oc1-orchestral/IdHub_E2E_testing
          ref: master
          token: ${{ secrets.SSIKIT_TOKEN }}

      - name: Install dependencies
        run: |
          npm ci

      - name: Install Playwright browsers
        run: |
          npx playwright install --with-deps

      - name: Run Playwright tests
        run: |
          npx playwright test