name: Release to PyPI

on:
  # Trigger on version tags
  push:
    tags:
      - 'v*.*.*'

  # Manual trigger with version input
  workflow_dispatch:
    inputs:
      version:
        description: 'Version to release (e.g., 1.0.1)'
        required: true
        type: string

  # Trigger when a GitHub release is published
  release:
    types: [published]

jobs:
  build-and-publish:
    runs-on: ubuntu-latest
    permissions:
      contents: write  # Required for creating GitHub releases
      id-token: write  # Required for trusted publishing to PyPI

    steps:
    - uses: actions/checkout@v4
      with:
        fetch-depth: 0  # Full history for changelog generation

    - name: Set up Python
      uses: actions/setup-python@v5
      with:
        python-version: '3.11'

    - name: Install uv
      uses: astral-sh/setup-uv@v3
      with:
        enable-cache: true
        cache-dependency-glob: "uv.lock"

    - name: Install dependencies
      run: |
        uv venv
        uv pip install build twine

    - name: Verify version matches tag
      if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/')
      run: |
        TAG_VERSION=${GITHUB_REF#refs/tags/v}
        PROJECT_VERSION=$(python -c "import tomllib; print(tomllib.load(open('pyproject.toml', 'rb'))['project']['version'])")
        if [ "$TAG_VERSION" != "$PROJECT_VERSION" ]; then
          echo "❌ Tag version ($TAG_VERSION) doesn't match project version ($PROJECT_VERSION)"
          exit 1
        fi
        echo "✅ Version verified: $PROJECT_VERSION"

    - name: Build package
      run: |
        source .venv/bin/activate
        python -m build

    - name: Check package
      run: |
        source .venv/bin/activate
        twine check dist/*

    - name: Publish to Test PyPI
      if: github.event_name == 'workflow_dispatch'
      env:
        TWINE_USERNAME: __token__
        TWINE_PASSWORD: ${{ secrets.TEST_PYPI_API_TOKEN }}
      run: |
        source .venv/bin/activate
        twine upload --repository testpypi dist/*
        echo "📦 Published to Test PyPI: https://test.pypi.org/project/pylint-sort-functions/"

    - name: Publish to PyPI
      if: github.event_name == 'release'  # Only on manual releases, not automatic tags
      env:
        TWINE_USERNAME: __token__
        TWINE_PASSWORD: ${{ secrets.PYPI_API_TOKEN }}
      run: |
        source .venv/bin/activate
        twine upload dist/*
        echo "📦 Published to PyPI: https://pypi.org/project/pylint-sort-functions/"

    - name: Create GitHub Release
      if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/')
      uses: softprops/action-gh-release@v1
      with:
        body: |
          ## Installation
          ```bash
          pip install pylint-sort-functions
          ```

          ## What's Changed
          See [CHANGELOG.md](https://github.com/hakonhagland/pylint-sort-functions/blob/main/CHANGELOG.md) for details.
        files: dist/*
        draft: false
        prerelease: false
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
