#!/usr/bin/env python3
"""Build or incrementally update the ClaudeTimeline database.

Default: one-shot incremental build, exit when done.

``--watch``: load the SentenceTransformer model ONCE, run the build, then
loop -- watch every profile's history.jsonl + per-session JSONL files for
changes and trigger an incremental rebuild whenever something moves. Ctrl+C
exits cleanly. Polling-based (stdlib only); no daemon, no IPC.
"""

import argparse

parser = argparse.ArgumentParser(description="Build SQLite DB from Claude Code history")
parser.add_argument("--full", action="store_true", help="Force full rebuild instead of incremental update")
parser.add_argument("--verbose", "-v", action="store_true")
parser.add_argument(
    "--watch", "-w",
    action="store_true",
    help=(
        "Load the embedding model once and rebuild on filesystem changes "
        "(stdlib polling, ~5s interval). Ctrl+C to exit."
    ),
)
parser.add_argument(
    "--poll-interval",
    type=float,
    default=5.0,
    help="Seconds between filesystem poll cycles in --watch mode (default: 5).",
)
parser.add_argument(
    "--debounce",
    type=float,
    default=2.0,
    help=(
        "Seconds of quiescence after the last detected change before "
        "kicking off a rebuild in --watch mode (default: 2). Coalesces "
        "rapid bursts of writes into a single rebuild."
    ),
)
parser.add_argument(
    "--no-embeddings",
    action="store_true",
    help="Skip the embeddings phase (and UMAP/clustering) entirely.",
)
parser.add_argument(
    "--fast",
    action="store_true",
    help=(
        "Search-only build: skip timeline events, idle gaps, virtual "
        "sessions, density cache, reattr, embeddings, and snapshot. "
        "Keeps all 5 ingest phases, account reconciliation, "
        "bash-executables, session branches, bash-results, "
        "searchable-items + FTS, and canaries."
    ),
)
args = parser.parse_args()

if args.fast:
    args.no_embeddings = True

if args.watch:
    # Deferred import: keeps the non-watch path's import surface unchanged
    # and avoids loading watch-only modules when nobody asked for them.
    from claude_timeline.build.watch import run_watch_loop
    run_watch_loop(
        full=args.full,
        verbose=args.verbose,
        poll_interval_s=args.poll_interval,
        debounce_s=args.debounce,
        skip_embeddings=args.no_embeddings,
        fast=args.fast,
    )
else:
    from claude_timeline.build.pipeline import run_build
    run_build(full=args.full, verbose=args.verbose, skip_embeddings=args.no_embeddings, fast=args.fast)
