import os
import re
import glob

files = glob.glob('templates/*/detail.html')

for filepath in files:
    with open(filepath, 'r') as f:
        content = f.read()

    # Find the entity name from the default_fields check
    entity_match = re.search(r'{% elif ([a-z_]+)\.default_fields is not none %}', content)
    if not entity_match:
        continue
    entity = entity_match.group(1)

    # 1. Update tags
    tag_search = r"{% elif " + entity + r"\.default_fields is not none %}\s*{% if 'special::tags' in " + entity + r"\.default_fields %}checked{% endif %}\s*{% else %}\s*checked\s*{% endif %}>\s*<label class=\"form-check-label text-white small\" for=\"includeTags\">Include</label>"
    
    tag_replace = r"{% elif " + entity + r".default_fields is not none %}\n                        {% if 'special::tags' in " + entity + r".default_fields %}checked{% endif %}\n                    {% endif %}>\n                    <label class=\"form-check-label text-white small {% if " + entity + r".default_fields is not none and 'special::tags' in " + entity + r".default_fields %}text-accent{% endif %}\" for=\"includeTags\">Include</label>"

    content = re.sub(tag_search, tag_replace, content)

    # 2. Update dt/dd blocks
    def dt_replacer(match):
        prefix_quote = match.group(1) # e.g. "wardrobe::" or "{{ section }}::"
        if '{{ section }}' in prefix_quote:
            prefix_logic = "(section ~ '::' ~ key)"
        else:
            p = prefix_quote.replace('::', '')
            prefix_logic = "('" + p + "::' ~ key)"

        res = f"{{% set is_default = {entity}.default_fields is not none and {prefix_logic} in {entity}.default_fields %}}\n"
        res += f"                                <dt class=\"col-sm-4 text-capitalize {{% if is_default %}}text-accent{{% endif %}}\">\n"
        res += f"                                    <input class=\"form-check-input me-1\" type=\"checkbox\" name=\"include_field\" value=\"{prefix_quote}{{{{ key }}}}\"\n"
        
        inner = match.group(2)
        inner = re.sub(r"{% if .+? in " + entity + r"\.default_fields %}checked{% endif %}", "{% if is_default %}checked{% endif %}", inner)
        
        res += inner
        res += f"\n                                    {{{{ key.replace('_', ' ') }}}}\n"
        res += f"                                    {{% if is_default %}}<span class=\"badge bg-primary ms-1\" style=\"font-size: 0.55rem; vertical-align: middle;\">DEF</span>{{% endif %}}\n"
        res += f"                                </dt>\n"
        res += f"                                <dd class=\"col-sm-8 {{% if is_default %}}text-accent{{% endif %}}\">{{{{ value if value else '--' }}}}</dd>"
        return res

    dt_pattern = r'<dt class="col-sm-4 text-capitalize">\s*<input class="form-check-input me-1" type="checkbox" name="include_field" value="(.*?){{ key }}"\s*(.*?)\s*{{ key\.replace\(\'_\', \' \'\) }}\s*</dt>\s*<dd class="col-sm-8">{{ value if value else \'--\' }}</dd>'

    content = re.sub(dt_pattern, dt_replacer, content, flags=re.DOTALL)

    with open(filepath, 'w') as f:
        f.write(content)
    
    print(f"Updated {filepath}")
