name: CI

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

jobs:
  test:
    name: Test (Python ${{ matrix.python-version }})
    runs-on: ubuntu-latest
    strategy:
      matrix:
        python-version: ["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 packages
        uses: actions/cache@v4
        with:
          path: ~/.cache/pip
          key: ${{ runner.os }}-pip-${{ matrix.python-version }}-${{ hashFiles('pyproject.toml') }}
          restore-keys: |
            ${{ runner.os }}-pip-${{ matrix.python-version }}-

      - name: Install dependencies
        run: pip install -e ".[dev]"

      - name: Run tests
        env:
          OAUTH_STORAGE_PATH: ${{ runner.temp }}/oauth-test-data
          MASTER_API_KEY: test-master-key-for-ci
        run: pytest tests/ -v --tb=short

      - name: Run tests with coverage
        if: matrix.python-version == '3.12'
        env:
          OAUTH_STORAGE_PATH: ${{ runner.temp }}/oauth-test-data
          MASTER_API_KEY: test-master-key-for-ci
        run: pytest tests/ --cov=core --cov=plugins --cov-report=xml --cov-report=term-missing -q

      - name: Upload coverage
        if: matrix.python-version == '3.12'
        uses: actions/upload-artifact@v4
        with:
          name: coverage-report
          path: coverage.xml

  lint:
    name: Lint & Format
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4

      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: "3.12"

      - name: Install dependencies
        run: pip install -e ".[dev]"

      - name: Check formatting (Black)
        run: black --check .

      - name: Lint (Ruff)
        run: ruff check .

  docker:
    name: Docker Build
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4

      - name: Build Docker image
        run: docker build -t mcphub:test .

      - name: Verify image was built
        run: docker image inspect mcphub:test > /dev/null
