name: CI

on:
  workflow_dispatch:
  pull_request:
  push:
    branches:
      - master
      - v*

jobs:
  linter:
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false

    steps:
    - name: Checkout
      uses: actions/checkout@v3

    - name: Set up Python 3.10
      uses: actions/setup-python@v4
      with:
        python-version: "3.10"

    - name: Install linter and formatter dependencies
      run: pip install -r requirements_developers.txt

    - name: Lint with flake8 
      run: flake8 src/ tests/

    - name: Check black format 
      run:  black --check --diff src/ tests/

  build-trd:
    strategy:
      fail-fast: false
      matrix:
        runs-on: [ubuntu-latest]
        python-version: ['3.7', '3.8', '3.9', '3.10']

    name: "🐍 ${{ matrix.python-version  }} • ${{ matrix.runs-on }}"
    runs-on: ${{ matrix.runs-on }}

    steps:
    - name: Checkout
      uses: actions/checkout@v3

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

    - name: Install software dependencies
      shell: bash
      run: |
        python -m pip install --upgrade pip
        sudo apt-get update -y
        sudo apt-get install graphviz -y
        pip install -r requirements.txt
        pip install -r requirements_developers.txt

    - name: Test with pytest
      run: pytest tests/

  build-documentation:
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false

    steps:
    - name: Checkout
      uses: actions/checkout@v3

    - name: Set up Python 3.10
      uses: actions/setup-python@v4
      with:
        python-version: "3.10"

    - name: Generate diagrams
      run: |
        sudo apt-get install graphviz -y
        pip install -r requirements.txt
        pip install -r requirements_developers.txt
        python docs/fsm/graphviz/draw_state_diagrams.py

    - name: Build documentation
      uses: ammaraskar/sphinx-action@master
      with:
        docs-folder: docs/
        pre-build-command: |
          mkdir -p docs/build/
          touch docs/build/.nojekyll
          pip install -r requirements_developers.txt
        build-command: sphinx-build -b html . build

  build-docker:
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false

    steps:
    - name: Checkout
      uses: actions/checkout@v3

    - name: Build the docker image
      run: docker build -t trdo/tezos-reward-distributor .