from __future__ import annotations

import json
import os
import sys
import time

sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))

from worklib.graphql_client import execute_graphql  # noqa: E402


def main() -> None:
    parent_id = "6915dc03462d5aea0adc8cbd"
    suffix = str(int(time.time()))
    created_parent_for_entrance: str | None = None
    for place_type in ("street", "flat", "entrance"):
        q = """
mutation ($place_type: PlaceType!, $names: [String!]!, $parent_id: String) {
  createPlaceMultiple(dto: {place_type: $place_type, names: $names, parent_id: $parent_id}) { id __typename }
}
""".strip()
        pid = created_parent_for_entrance if place_type == "entrance" and created_parent_for_entrance else parent_id
        variables = {"place_type": place_type, "names": [f"smoke-{place_type}-{suffix}"], "parent_id": pid}
        try:
            resp = execute_graphql(query=q, variables=variables)
        except Exception as e:  # noqa: BLE001
            print(place_type, "ERROR", type(e).__name__, e)
            continue
        print(place_type, json.dumps(resp, ensure_ascii=False)[:300])
        if place_type == "street":
            created = resp.get("data", {}).get("createPlaceMultiple")
            if isinstance(created, list) and created and isinstance(created[0], dict) and created[0].get("id"):
                created_parent_for_entrance = created[0]["id"]


if __name__ == "__main__":
    main()

