#!/usr/bin/env python3
import os
import re

README_PATH = "README.md"
START_MARKER = "<!-- BEGIN_STRUCTURE -->"
END_MARKER = "<!-- END_STRUCTURE -->"

def generate_structure():
    """Создаёт список файлов и папок в корне репозитория."""
    items = []
    for name in sorted(os.listdir(".")):
        if name.startswith('.') or name == 'scripts':
            continue
        icon = "📁" if os.path.isdir(name) else "📄"
        items.append(f"- {icon} {name}")
    return "\n".join(items)

def update_readme():
    with open(README_PATH, "r", encoding="utf-8") as f:
        content = f.read()

    new_block = generate_structure()
    pattern = re.compile(
        re.escape(START_MARKER) + r".*?" + re.escape(END_MARKER),
        re.DOTALL
    )
    replacement = START_MARKER + "\n" + new_block + "\n" + END_MARKER

    if pattern.search(content):
        new_content = pattern.sub(replacement, content)
    else:
        new_content = content.strip() + "\n\n" + replacement + "\n"

    with open(README_PATH, "w", encoding="utf-8") as f:
        f.write(new_content)

if __name__ == "__main__":
    update_readme()