from __future__ import annotations

from app.core.agent.processes.v2.workflows.doc_update_from_feature_v2.subprocesses.common.rules_catalog import (
    RulesCatalog,
)
from app.core.agent.processes.v2.workflows.doc_update_from_feature_v2.subprocesses.common.template_models import (
    TemplateSpec,
)
from app.core.agent.processes.v2.workflows.doc_update_from_feature_v2.subprocesses.common.template_parser import (
    TemplateParser,
)


class TemplateRegistry:
    def __init__(self, parser: TemplateParser | None = None) -> None:
        self._parser = parser or TemplateParser()

    def load(self, catalog: RulesCatalog, doc_type: str) -> TemplateSpec:
        template_name = f"templates/{doc_type}.template.md"
        template_text = catalog.template_text(doc_type)
        if not template_text.strip():
            raise ValueError(f"Template not found for doc_type '{doc_type}'")
        return self._parser.parse(template_name, template_text)
