"""An AI assistant that releases drafted work — with evidence.

Scenario: your AI drafts client-facing output (a filing, an email, a billing
entry). Today it just... goes out. This example adds the missing layer: every
release is gated, human sign-off is sealed, and anyone can verify the record
later without trusting you or Planisphere.

Run it:  pip install planisphere && python quickstart_agent.py ps_test_YOUR_KEY
"""

import sys

from planisphere_sdk import Planisphere, PlanisphereNeedsReview


def release_draft(ps: Planisphere, draft_id: str, summary: str) -> dict:
    """The one change to your agent: gate the release instead of just doing it."""
    try:
        decision = ps.require_allow(
            pack="law",
            proposed_action=f"Release drafted document {draft_id}: {summary}",
            surface="agent",
            source_key=f"agent:draft:{draft_id}",
            law_context={"matter_band": "active-litigation", "tool_id": "my-agent"},
            idempotency_key=f"release-{draft_id}",  # retries never double-bill
        )
    except PlanisphereNeedsReview as exc:
        decision = exc.decision
        print(f"  paused: routed to {decision['review_route']} — a human decides")
        # In production you'd stop here and resume on webhook/poll. For the
        # demo, the supervising human approves right now:
        ps.record_review(pack="law", action_key=decision["action_key"],
                         reviewer="supervising-partner", decision="approved")
        print("  approved by supervising-partner — sign-off sealed")
    return decision


def main() -> int:
    ps = Planisphere(sys.argv[1])  # key is positional
    decision = release_draft(ps, "draft-014", "Motion to compel, Johnson v. Dunn")

    # The part your auditor cares about: the receipt proves itself.
    verdict = ps.verify(decision["verify_request"])
    print(f"  receipt verified: {verdict['verified']} "
          f"(key_status: {verdict['key_status']})")
    print(f"  keep this action_key for your records: {decision['action_key']}")
    return 0


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