name: Tag and Release

on:
  push:
    branches: [main]
  workflow_dispatch:
    inputs:
      force_publish:
        description: "Publish to PyPI even if the tag already exists"
        type: boolean
        default: false

jobs:
  tag:
    name: Create tag from pyproject.toml
    runs-on: ubuntu-latest
    permissions:
      contents: write
    outputs:
      tag: ${{ steps.tag.outputs.tag }}
      created: ${{ steps.tag.outputs.created }}
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Read version from pyproject.toml
        id: version
        run: |
          VERSION=$(grep '^version = ' pyproject.toml | head -1 | sed 's/version = "\(.*\)"/\1/')
          echo "version=$VERSION" >> "$GITHUB_OUTPUT"

      - name: Create tag if it doesn't exist
        id: tag
        run: |
          TAG="v${{ steps.version.outputs.version }}"
          echo "tag=$TAG" >> "$GITHUB_OUTPUT"
          if git rev-parse "$TAG" >/dev/null 2>&1; then
            echo "Tag $TAG already exists — skipping."
            echo "created=false" >> "$GITHUB_OUTPUT"
          else
            git config user.name "github-actions[bot]"
            git config user.email "github-actions[bot]@users.noreply.github.com"
            git tag "$TAG"
            git push origin "$TAG"
            echo "Tag $TAG created."
            echo "created=true" >> "$GITHUB_OUTPUT"
          fi

  publish:
    name: Publish to PyPI
    needs: tag
    if: needs.tag.outputs.created == 'true' || inputs.force_publish == true
    runs-on: ubuntu-latest
    environment:
      name: pypi
      url: https://pypi.org/p/jujumate
    permissions:
      id-token: write
    steps:
      - uses: actions/checkout@v4
      - uses: astral-sh/setup-uv@v5
      - run: uv build
      - uses: pypa/gh-action-pypi-publish@release/v1

  release:
    name: Create GitHub Release
    needs: tag
    if: needs.tag.outputs.created == 'true'
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Create release with auto-generated notes
        env:
          GH_TOKEN: ${{ github.token }}
        run: |
          gh release create "${{ needs.tag.outputs.tag }}" \
            --title "${{ needs.tag.outputs.tag }}" \
            --generate-notes
