"""Read-only view of one bot's unsettled ``message_agent`` deliveries to local teammates.

The receipts already exist: every local delivery the sender's ``message_agent`` admits lands in
the TARGET profile's ``runtime/bot_live_delivery/<id>.json``, stamped ``author.id ==
"bot:<sender>"`` (``tools/bot_mode_dm.py``). This module only reads them, so the sender's Bot
Chat can show what each teammate is doing with the request: still queued, working on it, parked
on a question only the user can answer, or failed. Nothing here writes, claims, cancels or resends
a delivery — an outcome this reader cannot see is reported as unknown, never retried.
"""
from __future__ import annotations

import json
import logging
import time
from pathlib import Path
from typing import Any, Callable, Iterable, Optional

log = logging.getLogger(__name__)

# How long a failed delivery stays listed after its last receipt write. Long enough to come back
# to after lunch; Dismiss hides it sooner.
FAILED_VISIBLE_SECONDS = 24 * 3600
PREVIEW_CHARS = 140

_PENDING_RECEIPTS = frozenset({"queued", "claimed", "canonical"})
_FAILED_RECEIPTS = frozenset({"failed", "cancelled", "ambiguous"})
# ``session_admissions.status`` of the admission the receipt follows → strip state.
_ADMISSION_STATES = {"queued": "queued", "started": "working", "terminal": "claimed"}

# ``(admission_status | None, waiting_on | None)`` for one pending receipt; waiting_on is the kind
# of the target's open prompt (``"clarify"`` / ``"approval"``).
LiveState = Callable[[str, Path, dict], tuple[Optional[str], Optional[str]]]


def delivery_state(receipt_status: str, admission_status: str | None,
                   waiting_on: str | None) -> str | None:
    """The strip state of one receipt, or None when it is settled and drops off the strip.

    ``claimed`` covers the short windows where the target holds the delivery but its turn is not
    running on it: an admission that just finished while its receipt is still being written, or a
    failed attempt the owner is about to retry once."""
    if receipt_status in _FAILED_RECEIPTS:
        return "failed"
    if receipt_status not in _PENDING_RECEIPTS:
        return None
    if admission_status == "unknown":
        return "failed"
    if waiting_on and admission_status == "started":
        # The open prompt belongs to the turn running THIS delivery; one queued behind it in the
        # same chat is still just queued.
        return "waiting_on_you"
    if admission_status in _ADMISSION_STATES:
        return _ADMISSION_STATES[admission_status]
    return "claimed" if receipt_status == "claimed" else "queued"


def _failure_reason(record: dict, admission_status: str | None) -> str:
    """Typed reason for a failed row (``tools.bot_failure_reasons`` vocabulary where the owner
    wrote one). Unknown outcomes say so: the user must not resend those."""
    if record.get("status") == "ambiguous" or admission_status == "unknown":
        return "unknown_outcome"
    if record.get("status") == "cancelled":
        return str(record.get("reason") or "cancelled")
    return str(record.get("reason") or "unknown")


def _preview(message: Any) -> str:
    """The body the sender wrote, without the ``Message from 🤖 … (@x): `` signature."""
    text = message if isinstance(message, str) else ""
    head, sep, body = text.partition("): ")
    if sep and head.startswith("Message from "):
        text = body
    text = " ".join(text.split())
    return text if len(text) <= PREVIEW_CHARS else text[: PREVIEW_CHARS - 1] + "…"


def _read_receipt(path: Path) -> dict | None:
    """One receipt, or None for a file that is gone or is not a receipt (malformed, foreign).

    Any other read failure raises: skipping it would answer a shorter list, and the strip would
    drop a delivery that is still pending instead of keeping its last known row."""
    try:
        record = json.loads(path.read_text(encoding="utf-8-sig"))
    except FileNotFoundError:
        return None  # deleted between the scan and the read
    except ValueError:
        log.debug("bot_delivery_status: skipping malformed receipt %s", path, exc_info=True)
        return None
    return record if isinstance(record, dict) else None


def stored_admission_status(home: Path, admission_id: str) -> str | None:
    """``session_admissions.status`` read from the target's own store, read-only. None when the
    profile has no store or the row is gone."""
    from persistence_assembly import open_persistence_reader, profile_scope, store_exists

    if not admission_id or not store_exists(profile_scope(home=home)):
        return None
    with open_persistence_reader(home) as store:
        row = store.admissions.get(admission_id=admission_id)
    return row.status if row else None


def stored_live_state(_name: str, home: Path, record: dict) -> tuple[str | None, str | None]:
    """Default join when the target's authority is not in this process: the durable admission
    state only (an open prompt lives in the owner's memory, so it cannot be seen from here)."""
    return stored_admission_status(home, record.get("retry_admission_id") or record.get("admission_id") or ""), None


# receipts dir -> {file name: (stat identity, record)}. Receipts are kept on disk for recovery and
# replaced atomically (new inode) on every write, so an unchanged identity means unchanged content:
# a poll re-parses only what changed since the last one. A settled receipt is cached as its status
# alone, so the cache stays small however much history the directory holds.
_RECEIPT_CACHE: dict[Path, dict[str, tuple[tuple[int, int, int], dict]]] = {}


def _receipts(home: Path) -> list[tuple[Path, dict, float]]:
    """``(path, record, mtime)`` for every readable receipt under ``home``."""
    root = home / "runtime" / "bot_live_delivery"
    if not root.is_dir():
        return []
    seen = _RECEIPT_CACHE.get(root, {})
    fresh: dict[str, tuple[tuple[int, int, int], dict]] = {}
    out: list[tuple[Path, dict, float]] = []
    for path in root.glob("*.json"):
        try:
            st = path.stat()
        except OSError:
            continue
        identity = (st.st_ino, st.st_mtime_ns, st.st_size)
        hit = seen.get(path.name)
        if hit is not None and hit[0] == identity:
            record = hit[1]
        else:
            record = _read_receipt(path)
            if record is None:
                continue
            if str(record.get("status") or "") not in _PENDING_RECEIPTS | _FAILED_RECEIPTS:
                record = {"status": record.get("status")}
        fresh[path.name] = (identity, record)
        out.append((path, record, st.st_mtime))
    # Swapped whole, never mutated in place: a concurrent poll iterates its own snapshot.
    _RECEIPT_CACHE[root] = fresh
    return out


def list_sender_deliveries(
    root: Path, sender: str, *, live_state: LiveState = stored_live_state,
    now: float | None = None, profiles: Iterable[tuple[str, Path]] | None = None,
) -> list[dict]:
    """Every unsettled local delivery ``sender`` made, newest first.

    ``root`` is the install root (``bot_mode_probe._hermes_root``); ``profiles`` overrides the
    roster (``bot_mode_probe._roster``) for callers that already hold it. Settled replies drop
    off; failed ones stay for ``FAILED_VISIBLE_SECONDS``."""
    from tools.bot_mode_probe import _handle, _roster

    now = time.time() if now is None else now
    author_id = f"bot:{sender}"
    rows: list[dict] = []
    roster = list(_roster(root) if profiles is None else profiles)
    names = {name for name, _home in roster}
    for name, home in roster:
        if name == sender:
            continue
        for path, record, since in _receipts(Path(home)):
            status = str(record.get("status") or "")
            if status not in _PENDING_RECEIPTS | _FAILED_RECEIPTS:
                continue
            author = record.get("author")
            if not isinstance(author, dict) or author.get("id") != author_id:
                continue
            if status in _FAILED_RECEIPTS:
                if now - since > FAILED_VISIBLE_SECONDS:
                    continue
                admission, waiting_on = None, None
            else:
                admission, waiting_on = live_state(name, Path(home), record)
            state = delivery_state(status, admission, waiting_on)
            if state is None:
                continue
            row = {"delivery_id": path.stem, "target": name, "target_handle": _handle(name, names),
                   "state": state, "since": since, "preview": _preview(record.get("message"))}
            if state == "waiting_on_you":
                row["waiting_on"] = waiting_on
            if state == "failed":
                row["reason"] = _failure_reason(record, admission)
                if record.get("error"):
                    row["error"] = str(record["error"])[:500]
            rows.append(row)
    rows.sort(key=lambda r: r["since"], reverse=True)
    return rows
