"""
Prompt templates for SkyDiscover
"""

from pathlib import Path
from typing import Optional


class TemplateManager:
    """Manages templates with cascading override support"""

    def __init__(self, custom_template_dir: Optional[str] = None):
        # Get default template directory
        self.default_dir = Path(__file__).parent / "default" / "templates"
        self.custom_dir = Path(custom_template_dir) if custom_template_dir else None

        # Load templates with cascading priority
        self.templates = {}

        # 1. Load defaults
        self._load_from_directory(self.default_dir)

        # 2. Override with custom templates (if provided)
        if self.custom_dir and self.custom_dir.exists():
            self._load_from_directory(self.custom_dir)

    def _load_from_directory(self, directory: Path) -> None:
        """Load all templates from a directory"""
        if not directory.exists():
            return

        # Load .txt templates
        for txt_file in directory.glob("*.txt"):
            template_name = txt_file.stem
            with open(txt_file, "r") as f:
                self.templates[template_name] = f.read()

    def get_template(self, name: str) -> str:
        """Get a template by name"""
        if name not in self.templates:
            raise ValueError(f"Template '{name}' not found")
        return self.templates[name]