name: Run Linting and Unit Tests

on:
  pull_request:
    branches:
      - master
      - dev
  push:
    branches-ignore:
      - master
      - dev
  workflow_dispatch:


jobs:
  build:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        python-version: ["3.11", "3.12", "3.13"]

    steps:
      - uses: actions/checkout@v6
      - name: Set up Python ${{ matrix.python-version }}
        uses: actions/setup-python@v6
        with:
          python-version: ${{ matrix.python-version }}
      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install .
          pip install .[dev]
      - name: Lint with Ruff
        id: ruff
        run: |
          pip install ruff
          ruff check --output-format=github src/pyvesync
        continue-on-error: true
      - name: Run pylint
        id: pylint
        run: |
          pip install pylint
          pylint src/pyvesync --output-format=text --reports=n
        continue-on-error: true
      - name: Test with pytest
        id: pytest
        run: |
          pip install pytest
          pytest -v
      - name: Build docs (mkdocs)
        id: docs
        if: github.event_name == 'pull_request' && github.base_ref == 'master' && matrix.python-version == '3.12'
        run: |
          pip install .[docs]
          mkdocs build

      - name: Fail if any steps failed
        run: |
          failed=false

          if [ "${{ steps.ruff.outcome }}" = "failure" ]; then
            echo "::error::Ruff linting failed"
            failed=true
          fi

          if [ "${{ steps.pylint.outcome }}" = "failure" ]; then
            echo "::error::Pylint failed"
            failed=true
          fi

          if [ "${{ steps.pytest.outcome }}" = "failure" ]; then
            echo "::error::Pytest failed"
            failed=true
          fi

          if [ "$failed" = "true" ]; then
            echo "One or more checks failed. See errors above."
            exit 1
          else
            echo "All checks passed."
          fi