"""Parse mongo://collection/id artifact references from pipeline events."""

from __future__ import annotations

from dataclasses import dataclass


@dataclass(frozen=True)
class MongoReference:
    collection: str
    document_id: str

    @property
    def uri(self) -> str:
        return f"mongo://{self.collection}/{self.document_id}"


def parse_mongo_uri(value: str) -> MongoReference:
    raw = value.strip()
    if not raw.startswith("mongo://"):
        raise ValueError(f"invalid mongo URI: {value}")

    remainder = raw[len("mongo://") :]
    if "/" not in remainder:
        raise ValueError(f"invalid mongo URI: {value}")

    collection, document_id = remainder.split("/", 1)
    collection = collection.strip()
    document_id = document_id.strip()
    if not collection or not document_id:
        raise ValueError(f"invalid mongo URI: {value}")

    return MongoReference(collection=collection, document_id=document_id)
