name: CI

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

# Cancel in-progress runs for the same branch
concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  lint:
    name: Lint & Format
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Install uv
        uses: astral-sh/setup-uv@v4

      - name: Set up Python
        run: uv python install 3.11

      - name: Create venv and install Ruff
        run: |
          uv venv
          uv pip install ruff

      - name: Ruff Format Check
        run: uv run ruff format --check scripts/

      - name: Ruff Lint Check
        run: uv run ruff check scripts/

  security:
    name: Security Scan
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Install uv
        uses: astral-sh/setup-uv@v4

      - name: Set up Python
        run: uv python install 3.11

      - name: Create venv and install Bandit
        run: |
          uv venv
          uv pip install "bandit[toml]"

      - name: Run Bandit Security Scan
        run: uv run bandit -c scripts/pyproject.toml -r scripts/ --exclude scripts/tests/

  test:
    name: Unit Tests (Python ${{ matrix.python-version }})
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        python-version: ['3.10', '3.11', '3.12']
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Install uv
        uses: astral-sh/setup-uv@v4

      - name: Set up Python ${{ matrix.python-version }}
        run: uv python install ${{ matrix.python-version }}

      - name: Create venv and install dependencies
        run: |
          uv venv
          uv pip install -r scripts/requirements-dev.txt

      - name: Run pytest with coverage
        run: uv run pytest scripts/tests/ -v --tb=short --cov=scripts --cov-report=xml --cov-report=term-missing

      - name: Upload coverage to Codecov
        uses: codecov/codecov-action@v3
        with:
          files: ./coverage.xml
          flags: unittests
          name: codecov-python-${{ matrix.python-version }}
          fail_ci_if_error: false

  build:
    name: Build EPUB
    runs-on: ubuntu-latest
    needs: [lint, security, test]
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Install uv
        uses: astral-sh/setup-uv@v4

      - name: Set up Python
        run: uv python install 3.11

      - name: Create venv and install dependencies
        run: |
          uv venv
          uv pip install -r scripts/requirements-dev.txt

      - name: Build EPUB
        run: uv run scripts/build_epub.py

      - name: Verify EPUB Created
        run: |
          if [ -f claude-howto-guide.epub ]; then
            echo "EPUB built successfully"
            ls -lh claude-howto-guide.epub
          else
            echo "EPUB file not found!"
            exit 1
          fi

      - name: Upload EPUB Artifact
        uses: actions/upload-artifact@v4
        with:
          name: claude-howto-guide-epub
          path: claude-howto-guide.epub
          retention-days: 7
