"""Legacy builder module for backward compatibility."""

import logging
from pathlib import Path
from typing import Any

from .document_builder import DocumentBuilder
from .models import Agent, BuildConfig, BuildTarget, ClaudeDocument, Principle
from .project_builder import ProjectBuilder

# Configure logging
logger = logging.getLogger(__name__)


class ClaudeBuilder:
    """Legacy ClaudeBuilder class that delegates to the new architecture."""

    def __init__(self, snippets_dir: Path, templates_dir: Path):
        """Initialize ClaudeBuilder with backward compatibility.

        Args:
            snippets_dir: Directory containing snippet files
            templates_dir: Directory containing Jinja2 templates
        """
        self.snippets_dir = Path(snippets_dir)
        self.templates_dir = Path(templates_dir)
        logger.info(
            f"Initializing ClaudeBuilder with snippets_dir={snippets_dir}, "
            f"templates_dir={templates_dir}"
        )

        # Initialize the new architecture components
        self.document_builder = DocumentBuilder(snippets_dir)
        self.project_builder = ProjectBuilder(self.document_builder, templates_dir)

        logger.debug("Initialized with new architecture components")

    def load_snippet(self, snippet_path: str) -> str:
        """Load a markdown snippet from the snippets directory."""
        return self.document_builder.load_snippet(snippet_path)

    def load_agents(self) -> list[Agent]:
        """Load agent definitions from `agents.md` or fallback to `agents/` dir."""
        return self.document_builder.load_agents()

    def _load_agents_markdown(self) -> str:
        """Load raw agents markdown for template rendering."""
        return self.document_builder.load_agents_markdown()

    def build_document(self) -> ClaudeDocument:
        """Build the complete Claude document from snippets."""
        return self.document_builder.build_document()

    def _load_principles(self) -> list[Principle]:
        """Load principles from `fundamental_principles.md` or directory fallback."""
        return self.document_builder.load_principles()

    def _load_communication_patterns(self) -> dict[str, str]:
        """Load communication patterns."""
        return self.document_builder.load_communication_patterns()

    def _load_validation_system(self) -> dict[str, str]:
        """Load validation system components."""
        return self.document_builder.load_validation_system()

    def render_document(self, document: ClaudeDocument) -> str:
        """Render the document using the Jinja2 template."""
        return self.project_builder.render_document(document)

    def build_project(self, config: BuildConfig) -> None:
        """Build all targets in the project configuration."""
        return self.project_builder.build_project(config)

    def _build_target(self, target: BuildTarget, base_dir: Path) -> None:
        """Build a single target."""
        return self.project_builder._build_target(target, base_dir)

    def _render_template(self, template_name: str, data: Any) -> str:
        """Render a template with the given data."""
        return self.project_builder._render_template(template_name, data)

    def build_and_save(self, output_path: Path) -> None:
        """Build the document and save it to the output path."""
        return self.project_builder.build_and_save(output_path)

    def build_settings_data(self) -> dict[str, Any]:
        """Build settings.json data."""
        return self.project_builder.build_settings_data()

    def build_hook_data(self) -> dict[str, Any]:
        """Build hook script data."""
        return self.project_builder.build_hook_data()

    @staticmethod
    def create_default_config() -> BuildConfig:
        """Create a default build configuration with all targets."""
        return ProjectBuilder.create_default_config()

    @staticmethod
    def create_claude_md_only_config() -> BuildConfig:
        """Create a config that only builds CLAUDE.md (for backward compatibility)."""
        return ProjectBuilder.create_claude_md_only_config()

    # Legacy property access for backward compatibility
    @property
    def env(self):
        """Access to the Jinja2 environment for backward compatibility."""
        return self.project_builder.env
