"""Obol CLI entry point.

Usage:
    obol setup ob_live_xxxxxxxxxxxx     # One-command setup
    obol status
    obol whoami
    obol diagnose                       # Verify installation
    obol agents [list | get ID | score ID]
    obol compliance [status | report | profile | regulations | recompute]
    obol alerts [list | resolve ID | acknowledge ID]
    obol events [--agent ID | --action X]
    obol tail --agent ID [--interval N] [--action X]
    obol watch [--agent ID] [--interval N] [--no-events]
    obol export [events | compliance | audit] --format json|csv
    obol policies [list | create | delete | templates]
    obol config [show | set KEY VALUE]
    obol retry [--dry-run]
    obol unlock [--show]
    obol prune [--older-than N] [--dry-run] [--force]
    obol review [--session SID]         # Session detection summary
    obol explain <flag>                 # Explain a detection flag
    obol fp <alert-id> [--allow]        # Mark alert as false positive
    obol allow <flag> [--tool T]        # Manage detection allowlist
    obol override <code>                # One-time enforce bypass

Admin (GOR operators):
    obol admin monitor                  # 24/7 dashboard: VPS, API, alerts, events
    obol admin stats                    # Quick system stats
"""
import argparse
import sys


def main():
    parser = argparse.ArgumentParser(
        prog="obol",
        description="Obol - Trust infrastructure for AI agents",
        epilog="https://obol.grayowlresearch.com",
    )
    parser.add_argument(
        "--format", "-f",
        choices=["text", "json"],
        default="text",
        help="Output format (default: text)",
    )
    parser.add_argument(
        "--version", "-V",
        action="version",
        version="obol 0.1.0",
    )

    subparsers = parser.add_subparsers(dest="command", help="Available commands")

    # Status (default)
    from obol.cli.status import cmd_status
    from obol.cli._base import add_format_arg
    status_parser = subparsers.add_parser("status", help="Quick overview")
    status_parser.set_defaults(func=cmd_status)
    add_format_arg(status_parser)

    # Agents
    from obol.cli.agents import setup_parser as agents_setup
    agents_setup(subparsers)

    # Compliance
    from obol.cli.compliance import setup_parser as compliance_setup
    compliance_setup(subparsers)

    # Alerts
    from obol.cli.alerts import setup_parser as alerts_setup
    alerts_setup(subparsers)

    # Events
    from obol.cli.events import setup_parser as events_setup
    events_setup(subparsers)

    # Export
    from obol.cli.export_ import setup_parser as export_setup
    export_setup(subparsers)

    # Policies
    from obol.cli.policies import setup_parser as policies_setup
    policies_setup(subparsers)

    # Config
    from obol.cli.config import setup_parser as config_setup
    config_setup(subparsers)

    # Setup
    from obol.cli.setup import setup_parser as setup_setup
    setup_setup(subparsers)

    # Wrap
    from obol.cli.wrap import setup_parser as wrap_setup
    wrap_setup(subparsers)

    # Tail (live-follow events)
    from obol.cli.tail import setup_parser as tail_setup
    tail_setup(subparsers)

    # Whoami
    from obol.cli.whoami import setup_parser as whoami_setup
    whoami_setup(subparsers)

    # Retry failed events
    from obol.cli.retry import setup_parser as retry_setup
    retry_setup(subparsers)

    # Disable / Enable / Uninstall
    from obol.cli.disable import setup_parser as disable_setup
    disable_setup(subparsers)

    # Unlock (remove lockdown after tamper alert)
    from obol.cli.unlock import setup_parser as unlock_setup
    unlock_setup(subparsers)

    # Watch (live dashboard)
    from obol.cli.watch import setup_parser as watch_setup
    watch_setup(subparsers)

    # Diagnose (verify installation)
    from obol.cli.diagnose import setup_parser as diagnose_setup
    diagnose_setup(subparsers)

    # Repair (fix corrupted hooks)
    from obol.cli.repair import setup_parser as repair_setup
    repair_setup(subparsers)

    # Admin (operator tools)
    from obol.cli.admin import setup_parser as admin_setup
    admin_setup(subparsers)

    # Injection reports
    from obol.cli.injection import setup_parser as injection_setup
    injection_setup(subparsers)

    # Prune (clean old audit logs)
    from obol.cli.prune import setup_parser as prune_setup
    prune_setup(subparsers)

    # Review (session detection summary)
    from obol.cli.review import setup_parser as review_setup
    review_setup(subparsers)

    # Explain (detection flag explainer)
    from obol.cli.explain import setup_parser as explain_setup
    explain_setup(subparsers)

    # FP (mark alert as false positive)
    from obol.cli.fp import setup_parser as fp_setup
    fp_setup(subparsers)

    # Allow (local detection allowlist)
    from obol.cli.allow import setup_parser as allow_setup
    allow_setup(subparsers)

    # Override (one-time enforce mode bypass)
    from obol.cli.override import setup_parser as override_setup
    override_setup(subparsers)

    args = parser.parse_args()

    if not args.command:
        # Default to status if no command given
        try:
            cmd_status(args)
        except SystemExit:
            parser.print_help()
        return

    if hasattr(args, "func"):
        args.func(args)
    else:
        parser.print_help()


if __name__ == "__main__":
    main()
