from __future__ import annotations

from typing import Any

from niannian.storage.manifest import utc_now_iso


def refresh_feimao(entry: dict[str, Any], *, reachability: str | None = None) -> dict[str, Any]:
    """MVP session 续期；unreachable 时 skip 并保留 connected 态。"""
    if reachability == "unreachable":
        entry["refresh_skipped"] = "unreachable"
        entry["updated_at"] = utc_now_iso()
        return entry
    session = entry.get("session_token")
    if not session:
        entry["status"] = "needs_relogin"
        entry["error"] = "missing_session_token"
        return entry
    entry["status"] = "connected"
    entry["updated_at"] = utc_now_iso()
    entry.pop("error", None)
    entry.pop("refresh_skipped", None)
    return entry
