name: Build and publish python package

on:
  workflow_call:
    inputs:
      python_version:
        default: '3.13.8'
        description: 'Python version to use'
        required: false
        type: string
  workflow_dispatch:
    inputs:
      python_version:
        default: '3.13.8'
        description: 'Python version to use'
        required: true
        type: string

permissions: {}

jobs:
  build-package:
    runs-on: ubuntu-24.04
    steps:
      - name: Checkout arelle
        uses: actions/checkout@v5.0.0
        with:
          fetch-depth: 0
      - name: Install Python
        uses: actions/setup-python@v6.0.0
        with:
          cache: 'pip'
          check-latest: true
          python-version: ${{ inputs.python_version }}
      - name: Build python package
        run: |
          python -m pip install --upgrade pip setuptools wheel
          pip install build
          python -m build
      - name: Upload tar artifact
        uses: actions/upload-artifact@v4.6.2
        with:
          name: arelle.tar.gz
          if-no-files-found: error
          path: dist/*.tar.gz
      - name: Upload wheel artifact
        uses: actions/upload-artifact@v4.6.2
        with:
          name: arelle.whl
          if-no-files-found: error
          path: dist/*.whl

  publish-package:
    needs: build-package
    environment: release
    permissions:
      contents: write
    runs-on: ubuntu-24.04
    if: startsWith(github.ref, 'refs/tags')
    steps:
      - name: Download tar artifact
        uses: actions/download-artifact@v5.0.0
        with:
          name: arelle.tar.gz
      - name: Download wheel artifact
        uses: actions/download-artifact@v5.0.0
        with:
          name: arelle.whl
      - name: Publish package on release
        env:
          TWINE_USERNAME: ${{ secrets.TWINE_USERNAME }}
          TWINE_PASSWORD: ${{ secrets.TWINE_PASSWORD }}
          TWINE_REPOSITORY: ${{ github.repository == 'Arelle/Arelle' && 'pypi' || 'testpypi' }}
        run: pipx run twine upload ./*
      - name: Upload release artifact
        uses: softprops/action-gh-release@v2.4.1
        with:
          fail_on_unmatched_files: true
          files: './*'
