"""Build continuation snapshots from an existing event-day snapshot."""
from __future__ import annotations

import argparse
import asyncio
import json

from libs.common.config import get_settings
from libs.common.logging import configure_logging
from libs.export.continuation_snapshot import export_continuation_snapshot_from_base


async def run_continuation_snapshot(
    *,
    base_snapshot_dir: str,
    output_dir: str,
    snapshot_id: str,
    lookback_days: int,
) -> dict:
    return await export_continuation_snapshot_from_base(
        base_snapshot_dir=base_snapshot_dir,
        output_dir=output_dir,
        snapshot_id=snapshot_id,
        lookback_days=lookback_days,
    )


def main() -> None:
    parser = argparse.ArgumentParser(description="Build continuation snapshot from base snapshot")
    parser.add_argument("--base-snapshot-dir", required=True, help="Base snapshot directory path")
    parser.add_argument("--output-dir", default="./data/datasets/snapshots", help="Snapshot output root")
    parser.add_argument("--snapshot-id", required=True, help="New snapshot id")
    parser.add_argument("--lookback-days", type=int, default=3, help="Continuation signal lookback in trading days")
    parser.add_argument("--json", action="store_true", help="Print manifest JSON")
    args = parser.parse_args()

    configure_logging(get_settings().log_level)
    manifest = asyncio.run(
        run_continuation_snapshot(
            base_snapshot_dir=args.base_snapshot_dir,
            output_dir=args.output_dir,
            snapshot_id=args.snapshot_id,
            lookback_days=args.lookback_days,
        )
    )
    if args.json:
        print(json.dumps(manifest, indent=2))
    else:
        print(f"Snapshot exported: {manifest['snapshot_id']}")
        print(f"  Output: {manifest['output_dir']}")
        print(f"  Total rows: {manifest['total_rows']}")
        for split, count in manifest["row_counts"].items():
            print(f"  {split}: {count} rows")


if __name__ == "__main__":
    main()

