#!/bin/bash
# Bug Tracking CLI
# Usage: bugs <command> [options]

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
WATCHER_SCRIPT="/opt/agent-governance/testing/oversight/bug_watcher.py"

show_help() {
    echo "Bug Tracking CLI"
    echo ""
    echo "Usage: bugs <command> [options]"
    echo ""
    echo "Commands:"
    echo "  list                List all bugs"
    echo "  list --status open  Filter by status (open/in_progress/resolved)"
    echo "  list --severity high  Filter by severity (critical/high/medium/low)"
    echo "  get <id>            Get details of a specific bug"
    echo "  log <message>       Log a new bug"
    echo "  update <id> <status>  Update bug status"
    echo "  scan                Scan for anomalies"
    echo "  status              Show bug summary"
    echo ""
    echo "Examples:"
    echo "  bugs list --status open"
    echo "  bugs log -m 'API timeout in pipeline' --severity high"
    echo "  bugs update anom-abc123 resolved --notes 'Fixed in commit xyz'"
    echo "  bugs get anom-abc123"
    echo ""
}

case "$1" in
    list)
        shift
        python3 "$WATCHER_SCRIPT" list "$@"
        ;;
    get)
        shift
        if [ -z "$1" ]; then
            echo "Error: Bug ID required"
            echo "Usage: bugs get <bug-id>"
            exit 1
        fi
        python3 "$WATCHER_SCRIPT" get --id "$1"
        ;;
    log)
        shift
        python3 "$WATCHER_SCRIPT" log "$@"
        ;;
    update)
        shift
        if [ -z "$1" ] || [ -z "$2" ]; then
            echo "Error: Bug ID and status required"
            echo "Usage: bugs update <bug-id> <status> [--notes 'note']"
            exit 1
        fi
        BUG_ID="$1"
        STATUS="$2"
        shift 2
        python3 "$WATCHER_SCRIPT" update --id "$BUG_ID" --set-status "$STATUS" "$@"
        ;;
    scan)
        shift
        python3 "$WATCHER_SCRIPT" scan "$@"
        ;;
    status|summary)
        shift
        python3 "$WATCHER_SCRIPT" status "$@"
        ;;
    help|--help|-h)
        show_help
        ;;
    *)
        if [ -z "$1" ]; then
            show_help
        else
            echo "Unknown command: $1"
            echo "Run 'bugs help' for usage"
            exit 1
        fi
        ;;
esac
