import os
import re
from collections import defaultdict

def main():
    template_dir = os.path.join(os.path.dirname(__file__), "..", "app", "templates")
    t_pattern = re.compile(r"t\(\s*['\"]([^'\"]+?)['\"](?:\s*,\s*[^)]+?)?\s*\)")
    
    file_keys = defaultdict(set)
    for root, _, files in os.walk(template_dir):
        for f in files:
            if f.endswith(".html"):
                path = os.path.join(root, f)
                rel_path = os.path.relpath(path, template_dir).replace("\\", "/")
                with open(path, "r", encoding="utf-8", errors="ignore") as fp:
                    content = fp.read()
                    matches = t_pattern.findall(content)
                    for m in matches:
                        clean_key = m.strip()
                        if (
                            clean_key 
                            and not clean_key.startswith("privacy.")
                            and not clean_key.startswith("contact.")
                            and not clean_key.startswith("social.")
                            and not clean_key.startswith("donation.")
                            and not clean_key.startswith("{{")
                            and not clean_key.startswith("e.g.")
                            and clean_key not in {"en", "hi", "ur", "tab", "q", "type", "category", "state", "priority", "level", "notice", "appointment", "appreciation", "canvas", "iframe", "input", "permission", "recommendation", "youtube", "facebook", "instagram", "linkedin"}
                        ):
                            file_keys[rel_path].add(clean_key)
                            
    for file_name, keys in sorted(file_keys.items(), key=lambda x: len(x[1]), reverse=True):
        print(f"{file_name:40s}: {len(keys)} keys")

if __name__ == "__main__":
    main()
