name: Tests & Quality Checks

on:
  push:
    branches: [ main, develop ]
  pull_request:
    branches: [ main, develop ]

jobs:
  test:
    name: Test on Python ${{ matrix.python-version }}
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-latest, windows-latest, macos-latest]
        python-version: ["3.10", "3.11", "3.12"]
        exclude:
          # Reduce matrix to save CI time
          - os: ubuntu-latest
            python-version: "3.10"
          - os: macos-latest
            python-version: "3.10"

    steps:
    - uses: actions/checkout@v4
    
    - name: Set up Python ${{ matrix.python-version }}
      uses: actions/setup-python@v4
      with:
        python-version: ${{ matrix.python-version }}
    
    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        pip install -r requirements-dev.txt
    
    - name: Lint with ruff
      run: ruff check src tests
      continue-on-error: true
    
    - name: Format check with black
      run: black --check src tests
      continue-on-error: true
    
    - name: Type check with mypy
      run: mypy src/webdrop_bridge
      continue-on-error: true
    
    - name: Run pytest
      run: pytest --cov=src/webdrop_bridge --cov-report=xml
    
    - name: Upload coverage to Codecov
      uses: codecov/codecov-action@v3
      with:
        file: ./coverage.xml
        flags: unittests
        name: codecov-umbrella

  build:
    name: Build Artifacts
    runs-on: ${{ matrix.os }}
    needs: test
    if: success()
    strategy:
      matrix:
        include:
          - os: windows-latest
            artifact: webdrop-bridge-windows
          - os: macos-latest
            artifact: webdrop-bridge-macos

    steps:
    - uses: actions/checkout@v4
    
    - name: Set up Python
      uses: actions/setup-python@v4
      with:
        python-version: "3.11"
    
    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        pip install -r requirements-dev.txt
    
    - name: Build Windows MSI
      if: matrix.os == 'windows-latest'
      run: python build/scripts/build_windows.py
      continue-on-error: true
    
    - name: Build macOS DMG
      if: matrix.os == 'macos-latest'
      run: bash build/scripts/build_macos.sh
      continue-on-error: true
    
    - name: Upload artifacts
      uses: actions/upload-artifact@v3
      with:
        name: ${{ matrix.artifact }}
        path: build/dist/
        retention-days: 30
