from __future__ import annotations

from app.core.agent.processes.v2.workflows.doc_update_from_feature.doc_rules_pipeline.models import DocRulesBundle
from app.core.agent.processes.v2.workflows.doc_update_from_feature.doc_rules_pipeline.models import SelectedDocRules


class DocRulesSelector:
    _DEFAULT_SECTIONS: tuple[str, ...] = ("summary", "details", "tech-use-case", "fr", "requirements-format")
    _SECTIONS_BY_TYPE: dict[str, tuple[str, ...]] = {
        "api_method": ("summary", "details", "tech-use-case", "fr", "api-contract", "requirements-format"),
        "integration_doc": ("summary", "details", "tech-use-case", "fr", "api-contract", "requirements-format"),
        "ui_page": ("summary", "details", "tech-use-case", "fr", "requirements-format"),
        "logic_block": ("summary", "details", "tech-use-case", "fr", "requirements-format"),
        "architecture_overview": ("summary", "details", "tech-use-case", "fr", "requirements-format"),
        "domain_entity": ("summary", "details", "tech-use-case", "fr", "requirements-format"),
    }

    def select(self, bundle: DocRulesBundle, doc_type: str) -> SelectedDocRules | None:
        artifact = bundle.artifact_rules.get(doc_type, "").strip()
        template = bundle.templates.get(doc_type, "").strip()
        if not artifact or not template:
            return None
        names = self._SECTIONS_BY_TYPE.get(doc_type, self._DEFAULT_SECTIONS)
        section_texts = {name: bundle.sections.get(name, "") for name in names if bundle.sections.get(name, "").strip()}
        return SelectedDocRules(
            doc_type=doc_type,
            global_text=bundle.render_global(),
            artifact_text=artifact,
            template_text=template,
            section_texts=section_texts,
        )
