name: Build and create changelog PR

on:
  push:
    tags:
      - 'v*.*.*'

env:
  dists-artifact-name: python-package-distributions

jobs:
  changelog:
    runs-on: ubuntu-latest
    permissions:
      contents: write
      pull-requests: write
    steps:
      - name: Checkout repository
        uses: actions/checkout@v6
        with:
          fetch-depth: 0

      - name: Install git-cliff
        uses: taiki-e/install-action@v2
        with:
          tool: git-cliff

      - name: Generate changelog
        id: changelog
        run: |
          # Get the tag name
          TAG_NAME="${GITHUB_REF#refs/tags/}"

          # Generate changelog for this release only
          RELEASE_NOTES=$(git-cliff --config .git-cliff.toml --latest --strip header)

          # Prepend new release to existing CHANGELOG.md without regenerating entire file
          git-cliff --config .git-cliff.toml --latest --prepend CHANGELOG.md

          # Save release notes to output and file
          echo "notes<<EOF" >> $GITHUB_OUTPUT
          echo "$RELEASE_NOTES" >> $GITHUB_OUTPUT
          echo "EOF" >> $GITHUB_OUTPUT

          echo "version=${TAG_NAME}" >> $GITHUB_OUTPUT

      - name: Install uv
        uses: astral-sh/setup-uv@v7

      - name: Run pre-commit on generated CHANGELOG
        run: |
          # Run pre-commit to ensure the generated file is properly formatted
          uvx pre-commit run --files CHANGELOG.md || true

      - name: Create Pull Request for CHANGELOG
        uses: peter-evans/create-pull-request@v8
        with:
          token: ${{ secrets.CHANGELOG_AUTOMATION_TOKEN }}
          add-paths: |
            CHANGELOG.md
          commit-message: "chore(release): update CHANGELOG.md for ${{ steps.changelog.outputs.version }}"
          title: "chore(release): update CHANGELOG.md for ${{ steps.changelog.outputs.version }}"
          body: |
            Automated CHANGELOG.md update for release ${{ steps.changelog.outputs.version }}

            Generated by git-cliff from conventional commits.

            This PR was automatically created by the release workflow.
          branch: changelog-${{ steps.changelog.outputs.version }}
          base: main
          delete-branch: true
          author: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
          committer: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
          signoff: true
          labels: |
            changelog
            automated

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

      - name: Install the latest version of uv
        uses: astral-sh/setup-uv@v7
        with:
          enable-cache: true
          cache-dependency-glob: "pyproject.toml"
          github-token: ${{ secrets.GITHUB_TOKEN }}

      - name: Build package
        run: uv build --python 3.11 --python-preference only-managed --sdist --wheel . --out-dir dist

      - name: Check distributions
        run: uvx twine check dist/*

      - name: Store the distribution packages
        uses: actions/upload-artifact@v6
        with:
          name: ${{ env.dists-artifact-name }}
          path: dist/*
