# retoor <retoor@molodetz.nl>

from .core import datetime, db, get_table, timezone


def _ds_now() -> str:
    return datetime.now(timezone.utc).isoformat()


def create_deepsearch_session(
    uid: str,
    owner_kind: str,
    owner_id: str,
    query: str,
    depth: int,
    max_pages: int,
    collection: str,
) -> None:
    get_table("deepsearch_sessions").insert(
        {
            "uid": uid,
            "owner_kind": owner_kind,
            "owner_id": owner_id,
            "query": query,
            "status": "pending",
            "depth": depth,
            "max_pages": max_pages,
            "score": 0,
            "confidence": 0.0,
            "source_diversity": 0.0,
            "page_count": 0,
            "chunk_count": 0,
            "collection": collection,
            "summary": "",
            "created_at": _ds_now(),
            "completed_at": "",
            "deleted_at": None,
            "deleted_by": None,
        }
    )


def update_deepsearch_session(uid: str, fields: dict) -> None:
    if "deepsearch_sessions" not in db.tables:
        return
    payload = dict(fields)
    payload["uid"] = uid
    get_table("deepsearch_sessions").update(payload, ["uid"])


def get_deepsearch_session(uid: str) -> dict | None:
    if "deepsearch_sessions" not in db.tables:
        return None
    return get_table("deepsearch_sessions").find_one(uid=uid, deleted_at=None)


def add_deepsearch_message(
    uid: str, session_uid: str, role: str, content: str, citations: str = ""
) -> None:
    get_table("deepsearch_messages").insert(
        {
            "uid": uid,
            "session_uid": session_uid,
            "role": role,
            "content": content,
            "citations": citations,
            "created_at": _ds_now(),
            "deleted_at": None,
            "deleted_by": None,
        }
    )


def get_deepsearch_messages(session_uid: str, limit: int = 50) -> list[dict]:
    if "deepsearch_messages" not in db.tables:
        return []
    return list(
        get_table("deepsearch_messages").find(
            session_uid=session_uid,
            deleted_at=None,
            order_by=["created_at"],
            _limit=limit,
        )
    )


def get_cached_deepsearch_url(url_hash: str) -> dict | None:
    if "deepsearch_url_cache" not in db.tables:
        return None
    return get_table("deepsearch_url_cache").find_one(url_hash=url_hash)


def upsert_deepsearch_url_cache(
    url_hash: str,
    url: str,
    title: str,
    content_hash: str,
    status: int,
    byte_size: int,
) -> None:
    table = get_table("deepsearch_url_cache")
    existing = table.find_one(url_hash=url_hash)
    row = {
        "url_hash": url_hash,
        "url": url,
        "title": title,
        "content_hash": content_hash,
        "status": status,
        "byte_size": byte_size,
        "fetched_at": _ds_now(),
    }
    if existing:
        row["uid"] = existing["uid"]
        table.update(row, ["uid"])
    else:
        from devplacepy.utils import generate_uid

        row["uid"] = generate_uid()
        table.insert(row)
