from __future__ import annotations

import argparse
import logging
from pathlib import Path

from src.ingest.ingest import ingest_inbox
from src.logging_setup import log_event

logger = logging.getLogger(__name__)


def register(sub: argparse._SubParsersAction) -> None:
    ingest = sub.add_parser("ingest", help="Ingest photos from an inbox folder.")
    ingest.add_argument("--inbox", required=True, help="Inbox folder path.")
    ingest.add_argument("--out", required=True, help="Output folder path (manifest lives here).")
    ingest.add_argument("--dry-run", action="store_true", help="Do not write outputs/manifest.")
    ingest.set_defaults(_handler=_run)


def _run(args: argparse.Namespace) -> int:
    log_event(logger, "command.start", command="ingest")
    result = ingest_inbox(
        inbox_dir=Path(args.inbox),
        out_dir=Path(args.out),
        dry_run=bool(args.dry_run),
    )
    print(result.to_human_summary())
    log_event(logger, "command.complete", command="ingest")
    return 0
