name: CI

on: [push]

permissions:
  contents: read

jobs:
  unit-test:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        python-version: ['3.9', '3.10', '3.11', '3.12']
    
    steps:
    - uses: actions/checkout@v4
    
    - name: Set up Python ${{ matrix.python-version }}
      uses: actions/setup-python@v5
      with:
        python-version: ${{ matrix.python-version }}
    
    - name: Cache pip dependencies
      uses: actions/cache@v4
      with:
        path: ~/.cache/pip
        key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements*.txt') }}
        restore-keys: |
          ${{ runner.os }}-pip-
    
    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        pip install -r requirements-dev.txt
        pip install types-setuptools types-psutil
        pip install pytest-asyncio
          
    - name: Test with pytest
      env:
        CI: true
        PYTEST_CURRENT_TEST: true
      run: |
        pytest tests/ -k "not tui" -m "not hardware" --cov=src --cov-report=xml --cov-report=term-missing
      continue-on-error: true

    - name: Upload coverage reports to Codecov
      uses: codecov/codecov-action@v5
      with:
        token: ${{ secrets.CODECOV_TOKEN }}
        slug: ramseymcgrath/PCILeechFWGenerator
        files: ./coverage.xml  
        flags: unittests
        fail_ci_if_error: false


  tui-test:
    runs-on: ubuntu-latest
    needs: unit-test
    strategy:
      matrix:
        python-version: ['3.9', '3.10', '3.11', '3.12']
    
    steps:
    - uses: actions/checkout@v4
    
    - name: Set up Python ${{ matrix.python-version }}
      uses: actions/setup-python@v5
      with:
        python-version: ${{ matrix.python-version }}
    
    - name: Cache pip dependencies
      uses: actions/cache@v4
      with:
        path: ~/.cache/pip
        key: ${{ runner.os }}-pip-tui-${{ hashFiles('**/requirements*.txt') }}
        restore-keys: |
          ${{ runner.os }}-pip-
    
    - name: Install TUI test dependencies
      run: |
        python -m pip install --upgrade pip
        pip install -r requirements-dev.txt
        pip install "textual>=4.0.0" pytest-asyncio
        pip install types-setuptools types-psutil
          
    - name: Run TUI integration tests
      env:
        CI: true
        PYTEST_CURRENT_TEST: true
        # Ensure headless mode for textual
        TERM: xterm-256color
        TEXTUAL_HEADLESS: true
      run: |
        # Run TUI tests in headless mode
        pytest tests/test_tui_integration.py -v --tb=short -m tui
      continue-on-error: true


  integration:
    runs-on: ubuntu-latest
    needs: unit-test
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-python@v4
        with:
          python-version: '3.11'

      - name: Install dependencies
        run: |
          python -m venv .venv
          . .venv/bin/activate
          pip install --upgrade pip
          pip install -r requirements.txt -r requirements-test.txt

      - name: Install Podman
        run: |
          sudo apt-get update
          sudo apt-get install -y podman
          podman info

      - name: Create mock sysfs
        run: ./tests/create_mock_sysfs.sh tests/mock_sysfs

      - name: Run integration (host + podman)
        env:
          BOARD_NAME: pcileech_35t325_x1
        run: |
          . .venv/bin/activate
          bash tests/integration_smoke.sh

      - name: Upload integration logs
        if: always()
        uses: actions/upload-artifact@v4
        with:
          name: integration-logs
          path: tests/integration-logs/*

  docs:
    runs-on: ubuntu-latest
    
    steps:
    - uses: actions/checkout@v4
    
    - name: Set up Python
      uses: actions/setup-python@v5
      with:
        python-version: '3.11'
    
    - name: Install documentation dependencies
      run: |
        python -m pip install --upgrade pip
        pip install sphinx sphinx-rtd-theme
        pip install -r requirements.txt
    
    - name: Check documentation build
      run: |
        # Create basic Sphinx documentation structure if it doesn't exist
        if [ ! -f "docs/conf.py" ]; then
          mkdir -p docs
          sphinx-quickstart -q -p "PCILeech Firmware Generator" -a "Ramsey McGrath" -v "0.5.0" --ext-autodoc --ext-viewcode --makefile --no-batchfile docs
        fi
        # Build documentation
        cd docs && make html || echo "Documentation build test completed"