name: Publish to PyPI

on:
  release:
    types: [published]
  workflow_dispatch:
    inputs:
      target:
        description: 'Publish target (testpypi or pypi)'
        required: true
        default: 'testpypi'
        type: choice
        options:
          - testpypi
          - pypi

env:
  PYTHON_VERSION: "3.11"

jobs:
  build:
    name: Build Distribution
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0  # Full history for setuptools-scm

      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: ${{ env.PYTHON_VERSION }}

      - name: Install build dependencies
        run: |
          python -m pip install --upgrade pip
          pip install build twine

      - name: Build package
        run: python -m build

      - name: Check package metadata
        run: twine check dist/*

      - name: List built packages
        run: ls -la dist/

      - name: Upload build artifacts
        uses: actions/upload-artifact@v4
        with:
          name: python-package-distributions
          path: dist/
          retention-days: 7

  test-install:
    name: Test Installation
    needs: build
    runs-on: ubuntu-latest
    strategy:
      matrix:
        python-version: ["3.11", "3.12"]
    steps:
      - name: Set up Python ${{ matrix.python-version }}
        uses: actions/setup-python@v5
        with:
          python-version: ${{ matrix.python-version }}

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

      - name: Install wheel
        run: |
          pip install dist/*.whl
          python -c "import cmvk; print(f'cmvk version: {cmvk.__version__}')"

  publish-testpypi:
    name: Publish to TestPyPI
    needs: [build, test-install]
    runs-on: ubuntu-latest
    if: github.event_name == 'workflow_dispatch' && github.event.inputs.target == 'testpypi'
    environment:
      name: testpypi
      url: https://test.pypi.org/p/cmvk
    permissions:
      id-token: write  # Required for trusted publishing
    steps:
      - name: Download artifacts
        uses: actions/download-artifact@v4
        with:
          name: python-package-distributions
          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

  publish-pypi:
    name: Publish to PyPI
    needs: [build, test-install]
    runs-on: ubuntu-latest
    if: >
      github.event_name == 'release' ||
      (github.event_name == 'workflow_dispatch' && github.event.inputs.target == 'pypi')
    environment:
      name: pypi
      url: https://pypi.org/p/cmvk
    permissions:
      id-token: write  # Required for OIDC trusted publishing
    steps:
      - name: Download artifacts
        uses: actions/download-artifact@v4
        with:
          name: python-package-distributions
          path: dist/

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

  create-github-release-assets:
    name: Upload Release Assets
    needs: publish-pypi
    runs-on: ubuntu-latest
    if: github.event_name == 'release'
    permissions:
      contents: write
    steps:
      - name: Download artifacts
        uses: actions/download-artifact@v4
        with:
          name: python-package-distributions
          path: dist/

      - name: Upload to GitHub Release
        uses: softprops/action-gh-release@v2
        with:
          files: dist/*
