"""
Helpers to generate changelog information from Git history.
"""

from __future__ import annotations

from pkgmgr.core.git.queries import (
    GitChangelogQueryError,
    get_changelog,
)


def generate_changelog(
    cwd: str,
    from_ref: str | None = None,
    to_ref: str | None = None,
    include_merges: bool = False,
) -> str:
    """
    Generate a plain-text changelog between two Git refs.

    Returns a human-readable message instead of raising.
    """
    if to_ref is None:
        to_ref = "HEAD"

    rev_range = f"{from_ref}..{to_ref}" if from_ref else to_ref
    try:
        output = get_changelog(
            cwd=cwd,
            from_ref=from_ref,
            to_ref=to_ref,
            include_merges=include_merges,
        )
    except GitChangelogQueryError as exc:
        return (
            f"[ERROR] Failed to generate changelog in {cwd!r} "
            f"for range {rev_range!r}:\n{exc}"
        )

    if not output.strip():
        return f"[INFO] No commits found for range {rev_range!r}."

    return output.strip()
