"""Orbe recipe — run the same task across many profiles at once (a fleet).

Each profile gets its OWN `orbe.Trinh`, so each window types and moves with its own
per-seed persona. This is the point: the windows are NOT driven by one shared
rhythm — N identical keystroke timings across N windows would be a detectable batch
signature. With Orbe, every window looks like a different person. A pool limits how
many run concurrently (RAM-bound); each profile's discipline (rate caps, active
hours, checkpoint stop) is enforced independently.

Run:
    export ORBE_PROFILES="prof-1,prof-2,prof-3,prof-4"
    python examples/fleet_multi_profile.py
"""
import os
from concurrent.futures import ThreadPoolExecutor, as_completed

import orbe

CONCURRENT = 4          # how many profiles run at the same time (tune to your RAM)


def warm_up(profile: str) -> str:
    """One profile's task: open the feed and browse briefly, human-paced."""
    try:
        with orbe.Trinh(profile) as t:
            t.goto("https://example.com/home")
            for _ in range(4):
                t.scroll(800)
                t.wait(3)               # think-pause; timing varies per profile seed
                if "Verify" in t.read():
                    return f"{profile}: checkpoint — stopped"
        return f"{profile}: ok"
    except orbe.Blocked as e:
        return f"{profile}: blocked ({e})"


def main() -> int:
    profiles = [p.strip() for p in os.environ["ORBE_PROFILES"].split(",") if p.strip()]
    ok = 0
    with ThreadPoolExecutor(max_workers=CONCURRENT) as pool:
        futures = {pool.submit(warm_up, p): p for p in profiles}
        for fut in as_completed(futures):
            line = fut.result()
            print(line)
            ok += line.endswith(": ok")
    print(f"\n{ok}/{len(profiles)} finished cleanly.")
    return 0


# For a no-code, flow-based fleet (define a flow once, run it across profiles with a
# managed pool and per-flow survival stats), Orbe's launcher and flow engine provide
# a higher-level runner. This script shows the low-level SDK pattern.

if __name__ == "__main__":
    raise SystemExit(main())
