name: Release — PyPI + Zenodo

on:
  push:
    tags:
      - "v*.*.*"
  pull_request:
    branches: [main]

jobs:
  # -------------------------------------------------------------------------
  # CI — lint, test, coverage, docs
  # -------------------------------------------------------------------------
  ci:
    name: CI (${{ matrix.python-version }})
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        python-version: ["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 }}
          cache: pip

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

      - name: Ruff lint
        run: ruff check src tests

      - name: Ruff format check
        run: ruff format --check src tests

      - name: Run tests with coverage
        run: |
          pytest --cov=advanced_weighting_systems \
                 --cov-report=xml \
                 --cov-report=term-missing \
                 --cov-fail-under=99

      - name: Upload coverage to Codecov
        if: matrix.python-version == '3.11'
        uses: codecov/codecov-action@v4
        with:
          files: coverage.xml
          fail_ci_if_error: false

      - name: Build docs (strict)
        if: matrix.python-version == '3.11'
        run: mkdocs build --strict

  # -------------------------------------------------------------------------
  # Build — sdist + wheel
  # -------------------------------------------------------------------------
  build:
    name: Build distribution
    needs: ci
    runs-on: ubuntu-latest
    if: startsWith(github.ref, 'refs/tags/v')

    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-python@v5
        with:
          python-version: "3.11"

      - name: Install build
        run: pip install build

      - name: Build sdist and wheel
        run: python -m build

      - name: Upload dist artifacts
        uses: actions/upload-artifact@v4
        with:
          name: dist
          path: dist/

  # -------------------------------------------------------------------------
  # Publish to PyPI
  # -------------------------------------------------------------------------
  publish-pypi:
    name: Publish to PyPI
    needs: build
    runs-on: ubuntu-latest
    environment: pypi
    permissions:
      id-token: write

    steps:
      - name: Download dist artifacts
        uses: actions/download-artifact@v4
        with:
          name: dist
          path: dist/

      - name: Publish to PyPI (OIDC)
        uses: pypa/gh-action-pypi-publish@release/v1

  # -------------------------------------------------------------------------
  # Publish to Zenodo (via GitHub release)
  # -------------------------------------------------------------------------
  publish-zenodo:
    name: Publish to Zenodo
    needs: publish-pypi
    runs-on: ubuntu-latest
    if: startsWith(github.ref, 'refs/tags/v')

    steps:
      - uses: actions/checkout@v4

      - name: Download dist artifacts
        uses: actions/download-artifact@v4
        with:
          name: dist
          path: dist/

      - name: Create GitHub Release
        uses: softprops/action-gh-release@v2
        with:
          name: "v0.1.0 – Initial Release: Resonance-based symbolic coupling of heterogeneous neural networks in the AeonLayer"
          body_path: RELEASE_NOTES.md
          files: |
            dist/*.tar.gz
            dist/*.whl

      - name: Notify Zenodo via webhook
        env:
          ZENODO_TOKEN: ${{ secrets.ZENODO_TOKEN }}
        run: |
          echo "Zenodo will automatically mint a DOI via the GitHub-Zenodo integration."
          echo "Ensure the repository is connected to Zenodo at https://zenodo.org/account/settings/github/"

  # -------------------------------------------------------------------------
  # Deploy docs to GitHub Pages
  # -------------------------------------------------------------------------
  deploy-docs:
    name: Deploy documentation
    needs: ci
    runs-on: ubuntu-latest
    if: github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/v')
    permissions:
      contents: write

    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - uses: actions/setup-python@v5
        with:
          python-version: "3.11"

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

      - name: Deploy docs
        run: mkdocs gh-deploy --force --strict
