#!/usr/bin/env python3
"""CLI: Run the full pipeline."""

import argparse
import sys

from agent_samochodowy.config import Settings
from agent_samochodowy.dgred.client import DgredClient
from agent_samochodowy.ingestor.csv_source import CsvSource
from agent_samochodowy.logging_config import setup_logging
from agent_samochodowy.matcher.index import CatalogIndex
from agent_samochodowy.orchestrator import Pipeline


def main() -> int:
    parser = argparse.ArgumentParser(description="Run the FB parts agent pipeline")
    parser.add_argument("--source", required=True, help="Path to posts CSV/JSON file")
    parser.add_argument("--dry-run", action="store_true", help="Force dry-run mode")
    args = parser.parse_args()

    settings = Settings()
    if args.dry_run:
        settings.dry_run = True

    setup_logging(settings.log_level)

    source = CsvSource(args.source)
    index = CatalogIndex(settings.db_path)
    dgred = DgredClient(settings)

    try:
        pipeline = Pipeline(settings, source, index, dgred)
        stats = pipeline.run()
        print(f"\n{stats.summary()}")
        return 0
    finally:
        index.close()
        dgred.close()


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