import yaml
import os

LANG_LABELS = {
    "en": "english",
    "de": "deutsch",
}

with open("content.yaml", "r") as f:
    content = yaml.safe_load(f)

for page_name, translations in content.items():
    template_file = f"template-{page_name}.html"

    with open(template_file, "r") as f:
        template = f.read()

    languages = list(translations.keys())

    for lang, page_data in translations.items():
        output = template

        for key, value in page_data.items():
            output = output.replace(f"{{{{{key}}}}}", value)

        output = output.replace("{{lang}}", lang)

        links = []
        for other_lang in languages:
            if other_lang == lang:
                continue

            label = LANG_LABELS.get(other_lang, other_lang)
            link = f'<a class="navbar" href="../{other_lang}/{page_name}.html">{label}</a>'
            links.append(link)

        language_switcher = " ".join(links)
        output = output.replace("{{language-switcher}}", language_switcher)

        output_dir = f'public/{lang}'
        os.makedirs(output_dir, exist_ok=True)

        output_file = os.path.join(output_dir, f"{page_name}.html")

        with open(output_file, "w") as f:
            f.write(output)

        print(f"{output_file} generated.")