from pathlib import Path

from django.apps import apps
from django.conf import settings
from rich import print
from users.models import UserSettings


def extra_context(request):

    if not request.user.is_anonymous:
        user_settings, __ = UserSettings.objects.get_or_create(user=request.user)

    else:
        user_settings = None

    components = []
    for app in apps.get_app_configs():
        p = Path(settings.BASE_DIR) / app.name / "templates/components/"
        if p.exists():
            for path in p.iterdir():
                components.append(path.name)

    ret = {
        "user_settings": user_settings,
        "templates": {
            component: f"components/{component}/template.html"
            for component in components
        },
        "components": {
            component: f"components/{component}/vue.js" for component in components
        },
    }

    print(ret)
    return ret
