from __future__ import annotations

from app.modules.agent.task_runtime.models import TaskRuntimeContext


class AttachmentContextProvider:
    def build(self, ctx: TaskRuntimeContext) -> dict[str, object]:
        return {"attachments": list(ctx.attachments)}


class ConfluenceContextProvider:
    def build(self, ctx: TaskRuntimeContext) -> dict[str, object]:
        urls = [
            str(item.get("url"))
            for item in ctx.attachments
            if str(item.get("type") or "").lower() == "confluence_url" and item.get("url")
        ]
        return {"confluence_urls": urls}


class ContextEnrichmentService:
    def __init__(
        self,
        attachment_provider: AttachmentContextProvider | None = None,
        confluence_provider: ConfluenceContextProvider | None = None,
    ) -> None:
        self._attachment_provider = attachment_provider or AttachmentContextProvider()
        self._confluence_provider = confluence_provider or ConfluenceContextProvider()

    def enrich(self, ctx: TaskRuntimeContext) -> dict[str, object]:
        enriched: dict[str, object] = {}
        enriched.update(self._attachment_provider.build(ctx))
        enriched.update(self._confluence_provider.build(ctx))
        enriched["files"] = list(ctx.files)
        return enriched
