from pathlib import Path


class ReferenceDocsService:
    """Service to load and provide reference documents for agents."""

    def __init__(self, base_path: str | None = None):
        """
        Initialize the reference docs service.

        Args:
            base_path: Path to the reference_docs directory.
                      Defaults to ../reference_docs relative to backend/
        """
        if base_path is None:
            # Default to reference_docs at project root (sibling to backend/)
            base_path = Path(__file__).parent.parent.parent.parent / "reference_docs"
        self.base_path = Path(base_path)

        # Cache loaded documents
        self._brand_context: str | None = None
        self._channel_context: str | None = None

    def get_brand_context(self) -> str:
        """Load and return all brand guideline documents as a single context string."""
        if self._brand_context is None:
            brand_path = self.base_path / "brand"
            self._brand_context = self._load_all_markdown_files(brand_path)
        return self._brand_context

    def get_channel_context(self) -> str:
        """Load and return all channel guideline documents as a single context string."""
        if self._channel_context is None:
            channel_path = self.base_path / "channel"
            self._channel_context = self._load_all_markdown_files(channel_path)
        return self._channel_context

    def _load_all_markdown_files(self, directory: Path) -> str:
        """
        Load all .md files from a directory and concatenate them.

        Args:
            directory: Path to the directory containing markdown files

        Returns:
            Concatenated content of all markdown files with section headers
        """
        contents = []
        if directory.exists():
            # Sort files for consistent ordering
            for md_file in sorted(directory.glob("*.md")):
                try:
                    content = md_file.read_text(encoding="utf-8")
                    # Add file name as section header
                    contents.append(f"## {md_file.stem}\n\n{content}")
                except Exception as e:
                    print(f"Warning: Could not read {md_file}: {e}")

        if not contents:
            return "No reference documents found."

        return "\n\n---\n\n".join(contents)

    def get_context_summary(self) -> dict:
        """Return summary info about loaded documents."""
        brand_path = self.base_path / "brand"
        channel_path = self.base_path / "channel"

        brand_files = list(brand_path.glob("*.md")) if brand_path.exists() else []
        channel_files = list(channel_path.glob("*.md")) if channel_path.exists() else []

        return {
            "brand_files": [f.name for f in brand_files],
            "channel_files": [f.name for f in channel_files],
            "brand_context_length": len(self.get_brand_context()),
            "channel_context_length": len(self.get_channel_context()),
        }
