"""obol unlock - remove agent lockdown.

Removes ~/.obol/lockdown to restore agent operation after a tamper alert.

    obol unlock              Remove lockdown
    obol unlock --show       Show lockdown details without removing
"""
import json
import os

LOCKDOWN_PATH = os.path.expanduser("~/.obol/lockdown")


def cmd_unlock(args):
    if not os.path.exists(LOCKDOWN_PATH):
        print("Agent is not locked down.")
        return

    # Show lockdown info
    try:
        with open(LOCKDOWN_PATH) as f:
            info = json.load(f)
        print(f"Lockdown reason: {info.get('reason', 'Unknown')}")
        print(f"Target: {info.get('target', 'Unknown')}")
        print(f"Since: {info.get('timestamp', 'Unknown')}")
    except Exception:
        print("Lockdown file exists but could not be read.")

    if getattr(args, "show", False):
        return

    print()
    try:
        os.remove(LOCKDOWN_PATH)
        print("Lockdown removed. Agent can operate normally.")
    except Exception as e:
        print(f"Error removing lockdown: {e}")


def setup_parser(subparsers):
    parser = subparsers.add_parser("unlock", help="Remove agent lockdown")
    parser.add_argument("--show", action="store_true", help="Show lockdown details without removing")
    parser.set_defaults(func=cmd_unlock)
