from __future__ import annotations

import re

from gobby.storage.memories import Memory

# Pattern to match common bullet markers at start of string
_BULLET_PATTERN = re.compile(r"^[\s]*[-*•]\s*")


def _strip_leading_bullet(content: str) -> str:
    """
    Strip leading bullet points and whitespace from content.

    Handles common bullet markers: -, *, •
    Also strips any leading/trailing whitespace.

    Returns empty string if content is empty or only whitespace/bullets.
    """
    # Strip outer whitespace first
    content = content.strip()
    if not content:
        return ""

    # Remove leading bullet marker if present
    result = _BULLET_PATTERN.sub("", content)
    return result.strip()


def build_memory_context(memories: list[Memory]) -> str:
    """
    Build a formatted markdown context string from memories.

    Args:
        memories: List of Memory objects to include

    Returns:
        Formatted markdown string wrapped in <project-memory> tags
    """
    if not memories:
        return ""

    parts = ["<project-memory>"]

    # Group memories by type
    context_memories = [m for m in memories if m.memory_type == "context"]
    pref_memories = [m for m in memories if m.memory_type == "preference"]
    pattern_memories = [m for m in memories if m.memory_type == "pattern"]
    fact_memories = [m for m in memories if m.memory_type == "fact"]

    # 1. Project Context
    if context_memories:
        parts.append("## Project Context")
        for mem in context_memories:
            parts.append(mem.content)
        parts.append("")

    # 2. Preferences
    if pref_memories:
        parts.append("## Preferences")
        for mem in pref_memories:
            content = _strip_leading_bullet(mem.content)
            if content:  # Skip empty content
                parts.append(f"- {content}")
        parts.append("")

    # 3. Patterns
    if pattern_memories:
        parts.append("## Patterns")
        for mem in pattern_memories:
            content = _strip_leading_bullet(mem.content)
            if content:  # Skip empty content
                parts.append(f"- {content}")
        parts.append("")

    # 4. Facts/Other
    if fact_memories:
        parts.append("## Facts")
        for mem in fact_memories:
            content = _strip_leading_bullet(mem.content)
            if content:  # Skip empty content
                parts.append(f"- {content}")
        parts.append("")

    parts.append("</project-memory>")

    return "\n".join(parts)
