name: Release

on:
  release:
    types: [published]

jobs:
  pypi:
    name: Publish to PyPI
    runs-on: ubuntu-latest
    # Environment and permissions for trusted publishing
    environment: pypi
    permissions:
      id-token: write
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
        
      - name: Set up uv
        uses: astral-sh/setup-uv@v3
        
      - name: Install dependencies
        run: uv sync --dev
        
      - name: Run tests before release
        run: uv run pytest -v
        
      - name: Build package
        run: uv build
        
      - name: Verify build
        run: |
          ls -la dist/
          uv run twine check dist/*
        
      - name: Smoke test (wheel)
        run: |
          WHEEL=$(ls dist/*.whl)
          uv run --isolated --no-project -p 3.13 --with "$WHEEL" python -c "from chemformula import ChemFormula; print('Import successful'); water=ChemFormula('H2O'); print('Formula Weight:', water.formula_weight, 'g/mol')"

      - name: Publish to PyPI
        run: uv publish --trusted-publishing always
