"""Seed canonical normalized location registry rows.

Examples:
    python -m app.jobs.seed_master_locations --apply
"""

from __future__ import annotations

import argparse
import json
from typing import Any, Dict


def run_seed_master_locations(*, apply: bool = False) -> Dict[str, Any]:
    from db.location_registry import active_location_directory, ensure_default_home_surplus_locations
    from db.session import get_session

    with get_session() as session:
        result = ensure_default_home_surplus_locations(session)
        directory = active_location_directory(session)
        if not apply:
            session.rollback()
        return {"applied": bool(apply), "result": result, "locations": directory}


def main() -> None:
    parser = argparse.ArgumentParser(description="Seed canonical master location registry rows.")
    parser.add_argument("--apply", action="store_true", help="Commit changes. Without this flag the import is rolled back.")
    args = parser.parse_args()
    print(json.dumps(run_seed_master_locations(apply=args.apply), indent=2))


if __name__ == "__main__":
    main()
