import re


def fix_tabbed_content(page_html, page_key):
    """
    Tabbed content have <input> and <label> that are linked.

    When combining multiple pages into one, name duplicates occur.

    So in the example:

    <input checked="checked" id="__tabbed_1_1" name="__tabbed_1" type="radio">
    <label for="__tabbed_1_1">C</label>

    we should change:
    - <input> id, and <label> for attribute, to contain the pagekey.
    - <input> name, to make sure it's unique on print site

    So basically:
    <input checked="checked" id="{page_key}__tabbed_1_1" name="{page_key}__tabbed_1" type="radio">
    <label for="{page_key}__tabbed_1_1">C</label>
    """
    regex = re.compile(r"(\<input.*?name=\")([^\"]*?)(\".*?\>)", re.I)
    page_html = re.sub(regex, fr"\g<1>{page_key}-\g<2>\g<3>", page_html)

    regex = re.compile(r"(\<input.*?id=\")([^\"]*?)(\".*?\>)", re.I)
    page_html = re.sub(regex, fr"\g<1>{page_key}-\g<2>\g<3>", page_html)

    regex = re.compile(r"(\<label.*?for=\")([^\"]*?)(\".*?\>)", re.I)
    page_html = re.sub(regex, fr"\g<1>{page_key}-\g<2>\g<3>", page_html)

    return page_html