name: Publish to PyPI

on:
  push:
    tags:
      - "v*"
  workflow_dispatch:
    inputs:
      target:
        description: "Upload target"
        required: true
        default: "pypi"
        type: choice
        options:
          - pypi
          - testpypi

jobs:
  test:
    name: Tests (publish gate)
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

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

      - name: Install package + dev deps
        run: |
          pip install --upgrade pip
          pip install -e ".[dev]"

      - name: Run tests
        run: pytest tests/ -v --tb=short

  build:
    name: Build distribution
    runs-on: ubuntu-latest
    needs: test
    outputs:
      version: ${{ steps.meta.outputs.version }}
    steps:
      - uses: actions/checkout@v4

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

      - name: Install hatch
        run: pip install hatch

      - name: Read version from pyproject.toml
        id: meta
        run: |
          VERSION=$(python3 -c "import tomllib; print(tomllib.load(open('pyproject.toml','rb'))['project']['version'])")
          echo "version=$VERSION" >> "$GITHUB_OUTPUT"
          echo "Building version: $VERSION"

      - name: Build wheel + sdist
        run: hatch build

      - name: Verify dist contents
        run: ls -la dist/

      - name: Upload dist artifacts
        uses: actions/upload-artifact@v4
        with:
          name: dist-${{ steps.meta.outputs.version }}
          path: dist/
          retention-days: 7

  publish-pypi:
    name: Publish to PyPI
    needs: build
    runs-on: ubuntu-latest
    if: |
      (github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v')) ||
      (github.event_name == 'workflow_dispatch' && github.event.inputs.target == 'pypi')
    environment:
      name: pypi
      url: https://pypi.org/project/iil-authoringfw/
    permissions:
      id-token: write
    steps:
      - name: Download dist artifacts
        uses: actions/download-artifact@v4
        with:
          name: dist-${{ needs.build.outputs.version }}
          path: dist/

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

  publish-testpypi:
    name: Publish to TestPyPI
    needs: build
    runs-on: ubuntu-latest
    if: |
      github.event_name == 'workflow_dispatch' && github.event.inputs.target == 'testpypi'
    environment:
      name: testpypi
      url: https://test.pypi.org/project/iil-authoringfw/
    permissions:
      id-token: write
    steps:
      - name: Download dist artifacts
        uses: actions/download-artifact@v4
        with:
          name: dist-${{ needs.build.outputs.version }}
          path: dist/

      - name: Publish to TestPyPI
        uses: pypa/gh-action-pypi-publish@release/v1
        with:
          repository-url: https://test.pypi.org/legacy/
          print-hash: true
