from __future__ import annotations

import datetime as dt
from typing import Mapping

from ..interfaces import MetadataBuilder
from ..models import Chunk, DocumentIngestContext, DocumentIngestRequest


class DefaultMetadataBuilder(MetadataBuilder):
    """
    Augments chunk metadata with ingestion context.
    """

    def build(
        self,
        chunk: Chunk,
        request: DocumentIngestRequest,
        context: DocumentIngestContext,
    ) -> Mapping[str, object]:
        merged = dict(chunk.metadata)
        merged.setdefault("conversation_id", str(context.conversation_id))
        merged.setdefault("source_url", request.source_url)
        merged.setdefault("ingested_at", dt.datetime.utcnow().isoformat() + "Z")
        merged.setdefault("mime_type", request.mime_type)
        return merged
