import datetime

import msgspec


class OutlineDocument(msgspec.Struct):
    id: str
    title: str
    text: str
    url: str
    url_id: str = msgspec.field(name="urlId")
    collection_id: str = msgspec.field(name="collectionId")
    created_at: datetime.datetime = msgspec.field(name="createdAt")

    def __post_init__(self) -> None:
        doc_title_url_parsed = ""
        length = len(self.title)
        for i in range(length):
            char = self.title[i]
            next_char = self.title[i + 1] if (i + 1) < length else None
            if char.isspace() and next_char is not None and not next_char.isspace():
                doc_title_url_parsed += "-"
            elif char.isalnum():
                doc_title_url_parsed += char.lower()
        self.url += "doc/" + doc_title_url_parsed + "-" + self.url_id


class OutlineResult(msgspec.Struct):
    document: OutlineDocument
    context: str
    ranking: float

