from collections import defaultdict


def render(body: list[str], imports: set[tuple[str, str]]) -> str:
    # Group imports by module
    # {("module1", "Name1"), ("module1", "Name2"), ("module2", "Name3")} ->
    # {"module1": ["Name1", "Name2"], "module2": ["Name3"]}
    grouped: dict[str, list[str]] = defaultdict(list)
    for module, name in imports:
        grouped[module].append(name)

    lines = ["# This file was auto-generated by ezconfy. Do not edit manually."]
    for module in sorted(grouped):
        names = ", ".join(sorted(grouped[module]))
        lines.append(f"from {module} import {names}")
    lines.extend(body)
    return "\n".join(lines) + "\n"
