import argparse

from jinja2 import Template
from common import PROJECT

def render(base_project, subproject, internal, scm_url=None):
    version = base_project.rsplit(':', 1)[-1]
    context = {
        "base_project": subproject == "",
        "title": f"SUSE Edge {version} {subproject}".rstrip(),
        "ironic_base": "ISV:SUSE:Edge:Ironic" if internal else "Cloud:OpenStack",
    }
    if subproject == "ToTest":
        context["project"] = f"{base_project}:ToTest"
        context["description"] = (
            f"This project doesn't build, it stores a snapshot of SUSE Edge {version} "
            "project currently going through the automated test layer"
        )
        if "Factory" in base_project or internal:
            context["release_project"] = f"{base_project}:Snapshot"
    elif subproject == "Snapshot":
        context["project"] = f"{base_project}:Snapshot"
        context["release_project"] = f"{base_project.rsplit(':', 1)[0]}:Containers"
        context["for_release"] = True
        context["description"] = (
            f"This project doesn't build, it stores a snapshot of SUSE Edge {version} "
            "project that passed automated test layer"
        )
    elif subproject == "":
        context["project"] = base_project
        context["release_project"] = f"{base_project}:ToTest"
    else: # PR case direct python call
        context["base_project"] = True
        context["project"] = f"{base_project}:{subproject}"
        if scm_url is not None:
            context["scm_url"] = scm_url

    with open("_meta") as meta:
        template = Template(meta.read())
    return template.render(context)

def main():
    parser = argparse.ArgumentParser(
                    prog='ProgramName',
                    description='What the program does',
                    epilog='Text at the bottom of help')
    parser.add_argument("subproject", default="", choices=["", "ToTest", "Snapshot"], nargs="?")
    parser.add_argument("--internal", action="store_true")
    parser.add_argument("--pr")
    parser.add_argument("--scm-url")
    args = parser.parse_args()
    base_project = PROJECT.replace("isv", "ISV", 1) if args.internal else PROJECT

    print(render(
        base_project=base_project,
        subproject=args.subproject if args.pr is None else f"Staging:PR-{args.pr}",
        internal=args.internal,
        scm_url=args.scm_url,
    ))


if __name__ == "__main__":
    main()