from .models import SystemSetting
import os
import time

# Stabilize the timestamp to avoid cache-busting on every single request
# This will only change when the server restarts
STARTUP_TIMESTAMP = int(time.time())

def project_context(request):
    """
    Injects project description and social image URL from environment variables.
    Also injects a deployment timestamp for cache-busting.
    """
    return {
        "project_description": os.getenv("PROJECT_DESCRIPTION", ""),
        "project_image_url": os.getenv("PROJECT_IMAGE_URL", ""),
        "deployment_timestamp": STARTUP_TIMESTAMP,
    }

def global_settings(request):
    try:
        settings = SystemSetting.objects.first()
        if not settings:
            settings = SystemSetting.objects.create()
        return {
            'site_settings': settings,
            'global_settings': settings,
            'decimal_places': settings.decimal_places if settings else 3
        }
    except:
        return {'decimal_places': 3}
