from __future__ import annotations

from datetime import datetime, timezone
from pathlib import Path


class TraceFilePathBuilder:
    def __init__(self, root: Path) -> None:
        self._root = root

    def build_for_rag_session(self, rag_session_id: str | None) -> Path:
        self._root.mkdir(parents=True, exist_ok=True)
        stamp = datetime.now(timezone.utc).strftime("%Y%m%d-%H%M%S")
        suffix = self._suffix_from_rag_session(rag_session_id)
        return self._root / f"{stamp}-{suffix}.md"

    def _suffix_from_rag_session(self, rag_session_id: str | None) -> str:
        normalized = (rag_session_id or "").strip()
        if not normalized:
            return "no-rag-session"
        return normalized[-12:]
