"""Merge strategy configuration for plan three-way merges.

This module provides configurable merge strategies for resolving conflicts
when merging parent plans with subplans.
"""

from enum import StrEnum


class MergeStrategy(StrEnum):
    """Enumeration of available merge strategies for plan merging.

    Attributes:
        PREFER_PARENT: Auto-resolve conflicts by choosing parent values
        PREFER_SUBPLAN: Auto-resolve conflicts by choosing subplan values
        MANUAL: Surface conflicts for user resolution
    """

    PREFER_PARENT = "prefer-parent"
    """Auto-resolve conflicts in favor of parent plan."""

    PREFER_SUBPLAN = "prefer-subplan"
    """Auto-resolve conflicts in favor of subplan."""

    MANUAL = "manual"
    """Surface conflicts for manual user resolution."""

    def __str__(self) -> str:
        """Return the string representation of the merge strategy."""
        return self.value

    @classmethod
    def from_string(cls, value: str) -> "MergeStrategy":
        """Create a MergeStrategy from a string value.

        Args:
            value: The string representation of the merge strategy

        Returns:
            The corresponding MergeStrategy enum value

        Raises:
            ValueError: If the value is not a valid merge strategy
        """
        try:
            return cls(value)
        except ValueError:
            valid_values = ", ".join(str(s) for s in cls)
            raise ValueError(
                f"Invalid merge strategy '{value}'. Valid options are: {valid_values}"
            ) from None

    def is_auto_resolve(self) -> bool:
        """Check if this strategy auto-resolves conflicts.

        Returns:
            True if the strategy automatically resolves conflicts,
            False if conflicts need manual resolution
        """
        return self in (MergeStrategy.PREFER_PARENT, MergeStrategy.PREFER_SUBPLAN)

    def is_manual(self) -> bool:
        """Check if this strategy requires manual conflict resolution.

        Returns:
            True if conflicts need manual resolution, False otherwise
        """
        return self == MergeStrategy.MANUAL
