from __future__ import annotations

import argparse
import json

from db.brochure_taxonomy import reconcile_brochure_taxonomy_assignments
from db.session import get_session


def main() -> None:
    parser = argparse.ArgumentParser(description="Apply mapped brochure taxonomy groups to SKU listing-path assignments.")
    parser.add_argument("--brochure-collection-code", default=None, help="Limit to one brochure collection code.")
    parser.add_argument("--apply", action="store_true", help="Persist changes; default is dry-run.")
    parser.add_argument(
        "--keep-existing",
        action="store_true",
        help="Do not replace existing listing assignments for affected SKUs.",
    )
    args = parser.parse_args()

    with get_session() as session:
        result = reconcile_brochure_taxonomy_assignments(
            session,
            brochure_collection_code=args.brochure_collection_code,
            replace_existing=not args.keep_existing,
            dry_run=not args.apply,
        )
        if args.apply:
            session.commit()
    print(json.dumps(result, indent=2))


if __name__ == "__main__":
    main()
