name: Changelog Management

on:
  pull_request:
    types: [opened, synchronize, reopened]
  push:
    branches:
      - main
  release:
    types: [published]

jobs:
  check-changelog:
    name: Check for Changelog Entry
    runs-on: ubuntu-latest
    if: github.event_name == 'pull_request'
    steps:
      - uses: actions/checkout@v4

      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: "3.12"

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

      - name: Check for changelog entry
        run: |
          # Check if PR has a changelog entry
          if ! ls changes/*.md 2>/dev/null | grep -v -E '(README|\.template)\.md$'; then
            echo "::warning::No changelog entry found. Please add one with: uv run towncrier create <issue>.<type>.md"
            echo "Types: feature, bugfix, improvement, deprecation, breaking, security, performance, docs, dev, misc"
          fi

  preview-changelog:
    name: Preview Changelog Updates
    runs-on: ubuntu-latest
    if: github.event_name == 'pull_request'
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: "3.12"

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

      - name: Install dependencies
        run: uv sync --all-groups

      - name: Preview towncrier changes
        run: |
          echo "## 📝 Towncrier Preview (Manual Entries)" >> $GITHUB_STEP_SUMMARY
          echo "" >> $GITHUB_STEP_SUMMARY
          uv run towncrier build --draft >> $GITHUB_STEP_SUMMARY 2>/dev/null || echo "No new entries" >> $GITHUB_STEP_SUMMARY

      - name: Install git-cliff
        uses: kenji-miyake/setup-git-cliff@v2

      - name: Preview git-cliff changes
        run: |
          echo "" >> $GITHUB_STEP_SUMMARY
          echo "## 🔍 Git-cliff Preview (From Commits)" >> $GITHUB_STEP_SUMMARY
          echo "" >> $GITHUB_STEP_SUMMARY
          git cliff --unreleased --strip all >> $GITHUB_STEP_SUMMARY

  update-changelog:
    name: Update Changelog on Release
    runs-on: ubuntu-latest
    if: github.event_name == 'release'
    permissions:
      contents: write
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0
          token: ${{ secrets.GITHUB_TOKEN }}

      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: "3.12"

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

      - name: Install dependencies
        run: uv sync --all-groups

      - name: Install git-cliff
        uses: kenji-miyake/setup-git-cliff@v2

      - name: Update RELEASE_NOTES.md with towncrier
        run: |
          # Build the release notes with towncrier (removes fragment files)
          uv run towncrier build --version ${{ github.event.release.tag_name }}

      - name: Generate full changelog with git-cliff
        run: |
          # Generate comprehensive changelog from all commits
          git cliff --output CHANGELOG.md

      - name: Commit changelog updates
        run: |
          git config --local user.email "github-actions[bot]@users.noreply.github.com"
          git config --local user.name "github-actions[bot]"
          git add CHANGELOG.md RELEASE_NOTES.md changes/
          git commit -m "chore: update changelogs for ${{ github.event.release.tag_name }}" || echo "No changes to commit"
          git push
