name: Publish package to TestPyPI and PyPI

on:
  workflow_dispatch:
  push:
    tags:
      - "v[0-9].*"

jobs:
  build:
    name: Build package
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v4
      with:
        persist-credentials: false
        ref: main
        fetch-depth: 0

    - name: Get latest tag
      id: get_tag
      run: |
        if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
          # For manual triggers, get the latest tag on main
          LATEST_TAG=$(git describe --tags --abbrev=0 --match "v[0-9]*" origin/main)
          echo "tag=$LATEST_TAG" >> $GITHUB_OUTPUT
          git checkout $LATEST_TAG
        else
          # For tag pushes, use the tag that triggered the workflow
          echo "tag=${GITHUB_REF#refs/tags/}" >> $GITHUB_OUTPUT
        fi
    - name: Set up Python
      uses: actions/setup-python@v5
      with:
        python-version: "3.13"
    - name: Install pypa/build
      run: >-
        python3 -m
        pip install
        build
        --user
    - name: Build a binary wheel and a source tarball
      run: python3 -m build
    - name: Store the packages
      uses: actions/upload-artifact@v4
      with:
        name: python-package-distributions
        path: dist/

  testpypi:
    name: Publish package to TestPyPI
    needs:
    - build
    runs-on: ubuntu-latest

    environment:
      name: testpypi
      url: https://test.pypi.org/p/momapy

    permissions:
      id-token: write  # IMPORTANT: mandatory for trusted publishing

    steps:
    - name: Download all the dists
      uses: actions/download-artifact@v4
      with:
        name: python-package-distributions
        path: dist/
    - name: Publish package to TestPyPI
      uses: pypa/gh-action-pypi-publish@release/v1
      with:
        repository-url: https://test.pypi.org/legacy/

  pypi:
    name: >-
      Publish package to PyPI
    needs:
    - testpypi
    runs-on: ubuntu-latest
    environment:
      name: pypi
      url: https://pypi.org/p/momapy
    permissions:
      id-token: write  # IMPORTANT: mandatory for trusted publishing

    steps:
    - name: Download all the dists
      uses: actions/download-artifact@v4
      with:
        name: python-package-distributions
        path: dist/
    - name: Publish package to PyPI
      uses: pypa/gh-action-pypi-publish@release/v1

  changelog:
    name: Generate changelog
    needs:
    - build
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v4
      with:
        ref: main
        fetch-depth: 0

    - name: Get tag
      id: get_tag
      run: |
        if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
          LATEST_TAG=$(git describe --tags --abbrev=0 --match "v[0-9]*" origin/main)
          echo "tag=$LATEST_TAG" >> $GITHUB_OUTPUT
        else
          echo "tag=${GITHUB_REF#refs/tags/}" >> $GITHUB_OUTPUT
        fi

    - name: Generate changelog for this release
      uses: orhun/git-cliff-action@v4
      id: cliff
      with:
        config: cliff.toml
        args: --latest --strip header
      env:
        OUTPUT: CHANGELOG_RELEASE.md
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

    - name: Upload changelog snippet
      uses: actions/upload-artifact@v4
      with:
        name: changelog-snippet
        path: CHANGELOG_RELEASE.md

  github-release:
    name: Create GitHub Release
    needs:
    - pypi
    - changelog
    runs-on: ubuntu-latest

    permissions:
      contents: write

    steps:
    - name: Checkout to get tag info
      uses: actions/checkout@v4
      with:
        ref: main
        fetch-depth: 0

    - name: Get latest tag
      id: get_tag
      run: |
        if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
          LATEST_TAG=$(git describe --tags --abbrev=0 --match "v[0-9]*" origin/main)
          echo "tag=$LATEST_TAG" >> $GITHUB_OUTPUT
        else
          echo "tag=${GITHUB_REF#refs/tags/}" >> $GITHUB_OUTPUT
        fi

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

    - name: Download changelog snippet
      uses: actions/download-artifact@v4
      with:
        name: changelog-snippet

    - name: Extract version from tag
      run: |
        TAG="${{ steps.get_tag.outputs.tag }}"
        echo "VERSION=${TAG#v}" >> $GITHUB_ENV

    - name: Build release body
      run: |
        cat > RELEASE_BODY.md << 'HEADER'
        ## What's Changed
        HEADER
        cat CHANGELOG_RELEASE.md >> RELEASE_BODY.md
        cat >> RELEASE_BODY.md << EOF

        ## Installation

        \`\`\`bash
        pip install momapy==${{ env.VERSION }}
        \`\`\`

        ## Links

        - [Documentation](https://adrienrougny.github.io/momapy/)
        - [PyPI](https://pypi.org/project/momapy/${{ env.VERSION }}/)
        EOF

    - name: Create GitHub Release
      uses: softprops/action-gh-release@v2
      with:
        tag_name: ${{ steps.get_tag.outputs.tag }}
        files: dist/*
        body_path: RELEASE_BODY.md

  deploy-docs:
    name: Deploy documentation
    needs:
    - pypi
    runs-on: ubuntu-latest
    permissions:
      contents: write

    steps:
    - uses: actions/checkout@v4
      with:
        ref: main
        fetch-depth: 0

    - name: Get latest tag and checkout
      id: get_tag
      run: |
        if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
          LATEST_TAG=$(git describe --tags --abbrev=0 --match "v[0-9]*" origin/main)
          echo "tag=$LATEST_TAG" >> $GITHUB_OUTPUT
          git checkout $LATEST_TAG
        else
          echo "tag=${GITHUB_REF#refs/tags/}" >> $GITHUB_OUTPUT
        fi

    - name: Extract version from tag
      run: |
        TAG="${{ steps.get_tag.outputs.tag }}"
        echo "VERSION=${TAG#v}" >> $GITHUB_ENV
    - name: Configure Git Credentials
      run: |
        git config user.name github-actions[bot]
        git config user.email 41898282+github-actions[bot]@users.noreply.github.com
    - uses: actions/setup-python@v5
      with:
        python-version: 3.x
    - name: Cache dependencies
      uses: actions/cache@v4
      with:
        key: mkdocs-material-${{ github.run_id }}
        path: ~/.cache
        restore-keys: |
          mkdocs-material-
    - name: Install MkDocs and dependencies
      run: pip install mkdocs-material griffe-fieldz mkdocstrings-python griffe-inherited-docstrings mkdocs-jupyter mkdocs-pygments
    - name: Deploy documentation
      run: mkdocs gh-deploy --force
