#!/usr/bin/python3
from pathlib import Path

MASS_COUNT = 1000


def load_and_replace(
    template_fname: Path, out_fname: Path, placeholder: str, content: str
):
    out = []
    with open(template_fname, "r") as f:
        for line in f.readlines():
            line = line.replace(placeholder, content)
            out.append(line)

    with open(out_fname, "w") as f:
        f.writelines(out)


def main():
    ids = []

    for n in range(MASS_COUNT):
        id_ = f"{n:05d}"
        ids.append(id_)
        load_and_replace(
            template_fname=Path("app/domain/_machine_template.py"),
            out_fname=Path("app/domain").joinpath(f"mass_machine_{id_}.py"),
            placeholder="XTEMPLATEX",
            content=id_,
        )
        load_and_replace(
            template_fname=Path("app/controllers/_machine_template.py"),
            out_fname=Path("app/controllers").joinpath(f"mass_machine_{id_}.py"),
            placeholder="XTEMPLATEX",
            content=id_,
        )

    with open("__imports", "w") as f:
        for id_ in ids:
            f.write(f"from app.controllers.mass_machine_{id_} import MachineController_{id_}\n")

    routes = [f"MachineController_{id_}" for id_ in ids]
    with open("__routes", "w") as f:
        f.write(", ".join(routes))

if __name__ == "__main__":
    main()
