name: Release

on:
  release:
    types: [published]

jobs:
  lint:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6

      - uses: astral-sh/setup-uv@v7
        with:
          version: "latest"

      - name: Lint with ruff
        run: uvx ruff check drakkar/ tests/

      - name: Check formatting
        run: uvx ruff format --check drakkar/ tests/

  test:
    runs-on: ubuntu-latest
    needs: lint
    strategy:
      matrix:
        python-version: ["3.13", "3.14"]
    steps:
      - uses: actions/checkout@v4

      - uses: astral-sh/setup-uv@v7
        with:
          version: "latest"

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

      - name: Install dependencies
        run: uv sync --extra=dev

      - name: Type check with ty
        if: matrix.python-version == '3.14'
        run: uv run ty check drakkar/

      - name: Run tests with coverage
        run: uv run pytest --cov=drakkar --cov-report=term-missing

  verify-tag:
    runs-on: ubuntu-latest
    needs: test
    steps:
      - uses: actions/checkout@v6

      - uses: astral-sh/setup-uv@v7
        with:
          version: "latest"

      - name: Verify tag matches package version
        env:
          GIT_TAG: ${{ github.ref_name }}
        run: |
          TAG="${GIT_TAG#v}"
          PKG_VERSION=$(uv run python -c "from drakkar import __version__; print(__version__)")
          if [ "$TAG" != "$PKG_VERSION" ]; then
            echo "Error: tag v$TAG does not match __version__ = '$PKG_VERSION'"
            exit 1
          fi
          echo "Tag v$TAG matches package version $PKG_VERSION"

  publish:
    runs-on: ubuntu-latest
    needs: verify-tag
    environment: pypi
    permissions:
      id-token: write
    steps:
      - uses: actions/checkout@v6

      - uses: astral-sh/setup-uv@v7
        with:
          version: "latest"

      - name: Build package
        run: uv build

      - name: Verify build artifacts
        run: ls -la dist/

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