"""
One-off probe (Option B viability check).

Question: does the Page /feed come back ordered by `updated_time` (i.e. bumped
to the top when a post gets a new comment) or just by `created_time`?

- If it's activity-ordered, we can poll newest-activity-first and STOP once we
  pass the last run time -> a tiny per-run window that still catches new comments
  on OLD posts. (Option B)
- If it's creation-ordered, early-stop-by-activity isn't reliable and we fall
  back to a bounded recent window.

Run once:  python probe_feed_order.py
"""

from download_comments import fb_get, PAGE_ID


def main():
    params = {"fields": "id,created_time,updated_time", "limit": 50}
    data = fb_get(f"{PAGE_ID}/feed", params)
    posts = data.get("data", [])

    if not posts:
        print("No posts returned — check the token / error above.")
        return

    print(f"{'#':<3} {'created_time':<26} {'updated_time':<26} bumped?")
    for i, p in enumerate(posts, 1):
        ct = p.get("created_time", "") or ""
        ut = p.get("updated_time", "") or ""
        bumped = "YES (activity after post)" if (ut and ct and ut[:19] != ct[:19]) else ""
        print(f"{i:<3} {ct:<26} {ut:<26} {bumped}")

    uts = [(p.get("updated_time") or "") for p in posts]
    cts = [(p.get("created_time") or "") for p in posts]
    sorted_by_ut = all(uts[i] >= uts[i + 1] for i in range(len(uts) - 1))
    sorted_by_ct = all(cts[i] >= cts[i + 1] for i in range(len(cts) - 1))
    has_ut = all(bool(u) for u in uts)
    any_bumped = any(uts[i][:19] != cts[i][:19] for i in range(len(posts)) if uts[i] and cts[i])

    print()
    print(f"updated_time present on all posts? {has_ut}")
    print(f"returned order sorted by updated_time desc? {sorted_by_ut}")
    print(f"returned order sorted by created_time desc? {sorted_by_ct}")
    print(f"any post bumped (updated_time > created_time)? {any_bumped}")
    print()

    if has_ut and sorted_by_ut and any_bumped and not sorted_by_ct:
        print("=> ACTIVITY-ordered. Option B (early-stop polling) is viable.")
    elif sorted_by_ct and not any_bumped:
        print("=> CREATION-ordered. Early-stop-by-activity NOT reliable; use bounded floor window.")
    else:
        print("=> Ambiguous — inspect the rows above (look for an OLD created_time")
        print("   sitting near the TOP with a RECENT updated_time).")


if __name__ == "__main__":
    main()
