"""Remove channel assignments for master SKUs not linked to the remote catalog.

Use after an accidental bulk assign (e.g. link_master_skus --assign-all) when you
only want ~2k matched Magento/Shopify/Plytix SKUs in push scope, not all 8701 masters.

Keeps assignments where an active channel_sku_mapping exists and either:
  - channel_sku is present in the pulled catalog (magento_catalog_state / product_snapshot), or
  - remote_id is set on the mapping (Shopify links from link_master_skus).

CLI:
    python -m app.jobs.prune_channel_assignments --dry-run
    python -m app.jobs.prune_channel_assignments --no-dry-run
    python -m app.jobs.prune_channel_assignments --no-dry-run --channels magento
    python -m app.jobs.prune_channel_assignments --no-dry-run --prune-bogus-mappings
"""

from __future__ import annotations

import argparse
import json
import sys

from db.channel_assignment_prune import prune_channels
from db.session import get_session


def run_prune_channel_assignments(
    *,
    dry_run: bool = True,
    channels: list[str] | None = None,
    magento_connection_id: int | None = None,
    shopify_connection_id: int | None = None,
    prune_bogus_mappings: bool = False,
) -> dict:
    selected = channels or ["magento", "shopify", "plytix"]
    with get_session() as session:
        summary = prune_channels(
            session,
            channels=selected,
            magento_connection_id=magento_connection_id,
            shopify_connection_id=shopify_connection_id,
            prune_bogus_mappings=prune_bogus_mappings,
            dry_run=dry_run,
        )
        if not dry_run:
            session.commit()
    return summary


def main() -> int:
    parser = argparse.ArgumentParser(
        description="Unassign master SKUs that are not linked to the pulled remote catalog"
    )
    parser.add_argument("--dry-run", dest="dry_run", action="store_true", default=True)
    parser.add_argument(
        "--no-dry-run",
        dest="dry_run",
        action="store_false",
        help="Apply assignment removals",
    )
    parser.add_argument(
        "--channels",
        nargs="+",
        default=None,
        choices=["magento", "shopify", "plytix"],
        help="Channels to prune (default: all three)",
    )
    parser.add_argument("--magento-connection-id", type=int, default=None)
    parser.add_argument("--shopify-connection-id", type=int, default=None)
    parser.add_argument(
        "--prune-bogus-mappings",
        action="store_true",
        help="Also deactivate identity 1:1 mappings not present on the remote catalog",
    )
    args = parser.parse_args()

    summary = run_prune_channel_assignments(
        dry_run=args.dry_run,
        channels=args.channels,
        magento_connection_id=args.magento_connection_id,
        shopify_connection_id=args.shopify_connection_id,
        prune_bogus_mappings=args.prune_bogus_mappings,
    )
    print(json.dumps(summary, indent=2, default=str))
    return 0 if summary.get("status") == "ok" else 1


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