"""
EmptyPatchCritic implementation.

This critic only evaluates whether a git patch is non-empty.
Unlike AgentFinishedCritic, it does not check for proper agent completion.
"""

from collections.abc import Sequence

from openhands.sdk.critic.base import CriticBase, CriticResult
from openhands.sdk.event import LLMConvertibleEvent
from openhands.sdk.logger import get_logger


logger = get_logger(__name__)


class EmptyPatchCritic(CriticBase):
    """
    Critic that only evaluates whether a git patch is non-empty.

    This critic checks only one criterion:
    - The generated git patch is non-empty (actual changes were made)

    Unlike AgentFinishedCritic, this critic does not check for proper
    agent completion with FinishAction.
    """

    def evaluate(
        self,
        events: Sequence[LLMConvertibleEvent],  # noqa: ARG002
        git_patch: str | None = None,
    ) -> CriticResult:
        """
        Evaluate if a git patch is non-empty.

        Args:
            events: List of events from the agent's execution (not used)
            git_patch: Optional git patch generated by the agent

        Returns:
            CriticResult with score 1.0 if patch is non-empty, 0.0 otherwise
        """
        if not git_patch or not git_patch.strip():
            logger.debug("EmptyPatchCritic: Empty git patch")
            return CriticResult(score=0.0, message="Git patch is empty or missing")

        logger.debug("EmptyPatchCritic: Non-empty git patch found")
        return CriticResult(score=1.0, message="Git patch is non-empty")
