"""Deactivate invalid SEO intersections under hub/shopping-facet product taxonomy.

Examples:
  python -m app.jobs.deactivate_facet_rooted_seo_intersections --dry-run
  python -m app.jobs.deactivate_facet_rooted_seo_intersections --apply \\
      --hub-path-slug kitchen-cabinets
"""

from __future__ import annotations

import argparse
import json
import sys

from db.listing_intersections import deactivate_shopping_facet_rooted_intersections
from db.session import get_session


def main() -> int:
    parser = argparse.ArgumentParser(
        description=(
            "Deactivate SEO listing paths/rules nested under shopping facets "
            "(e.g. kitchen-cabinets/accessories/clearance-kit/base-cabinets/...). "
            "Keeps the real product leaf remote (clearance-kit) intact."
        )
    )
    parser.add_argument("--hub-path-slug", default="kitchen-cabinets")
    parser.add_argument("--apply", action="store_true", help="Persist deactivations")
    parser.add_argument("--dry-run", action="store_true", help="Preview only (default unless --apply)")
    args = parser.parse_args()

    dry_run = not args.apply
    with get_session() as session:
        result = deactivate_shopping_facet_rooted_intersections(
            session,
            dry_run=dry_run,
            hub_path_slug=args.hub_path_slug,
        )
        if not dry_run:
            session.commit()
    print(json.dumps(result, indent=2, default=str))
    return 0


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