"""CLI for the v0 orchestrator: observe a pid for a fixed window."""

from __future__ import annotations

import argparse
import logging
import sys
from pathlib import Path

from .episode import EpisodeConfig, EpisodeRunner


def main() -> int:
    parser = argparse.ArgumentParser(
        prog="cis490-orchestrator",
        description="Run a single episode against a target pid.",
    )
    parser.add_argument(
        "--target-pid",
        type=int,
        required=True,
        help="pid to sample (later this will be the qemu-system pid)",
    )
    parser.add_argument(
        "--duration",
        type=float,
        default=10.0,
        help="seconds to observe (default 10)",
    )
    parser.add_argument(
        "--interval-ms",
        type=int,
        default=100,
        help="sampling interval (default 100ms = 10 Hz)",
    )
    parser.add_argument(
        "--data-root",
        default="data",
        help="output directory root (default ./data)",
    )
    parser.add_argument(
        "--episode-id",
        default=None,
        help="override ULID generation (mostly for tests)",
    )
    args = parser.parse_args()

    logging.basicConfig(
        level=logging.INFO,
        format="%(asctime)s %(levelname)s %(name)s %(message)s",
    )

    cfg = EpisodeConfig(
        target_pid=args.target_pid,
        duration_s=args.duration,
        interval_ms=args.interval_ms,
        data_root=Path(args.data_root),
        episode_id=args.episode_id,
    )
    result = EpisodeRunner(cfg).run()

    print(f"episode_id={result.episode_id}")
    print(f"path={result.episode_dir}")
    print(f"rows_proc={result.rows_proc}")
    print(f"duration_observed_s={result.duration_observed_s:.2f}")
    return 0 if not result.pid_disappeared else 1


if __name__ == "__main__":
    sys.exit(main())
