import importlib
import re

from proto.enums import ProtoEnumMeta

from google.ads.googleads import client

enums_module = importlib.import_module(
    f"google.ads.googleads.{client._DEFAULT_VERSION}.enums"
)


lines = []
for enum in enums_module.__all__:
    enum_class = getattr(enums_module, enum)
    for attr in dir(enum_class):
        attr_val = getattr(enum_class, attr)
        if isinstance(attr_val, ProtoEnumMeta):
            lines.append(
                f"    {enum}: type[{client._DEFAULT_VERSION}.enums.{enum}.{attr_val.__name__}]"
            )
            break

with open("google-stubs/ads/googleads/client.pyi") as f:
    client_pyi = f.read()
lines.insert(0, "# Autogenerated enums")
lines.append("    # End of autogenerated enums")
client_pyi = re.sub(
    "# Autogenerated enums.+# End of autogenerated enums",
    "\n".join(lines),
    client_pyi,
    flags=re.DOTALL,
)
with open("google-stubs/ads/googleads/client.pyi", mode="w") as f:
    f.write(client_pyi)
