"""
App-level settings with sensible defaults.

Override in your Django settings.py:
    BAMBU_RUN_TIMEZONE = 'Australia/Melbourne'
    BAMBU_RUN_BASE_TEMPLATE = 'base/base.html'
"""

from django.conf import settings


def get_setting(name, default):
    return getattr(settings, name, default)


# Timezone for all timestamp display and queries
BAMBU_RUN_TIMEZONE = property(lambda self: get_setting("BAMBU_RUN_TIMEZONE", "UTC"))

# Base template that all bambu_run templates extend
BAMBU_RUN_BASE_TEMPLATE = property(
    lambda self: get_setting("BAMBU_RUN_BASE_TEMPLATE", "bambu_run/base.html")
)

# Login URL for @login_required redirects
BAMBU_RUN_LOGIN_URL = property(
    lambda self: get_setting("BAMBU_RUN_LOGIN_URL", "/accounts/login/")
)

# Default brand for auto-created filaments from MQTT
BAMBU_RUN_AUTO_CREATE_BRAND = property(
    lambda self: get_setting("BAMBU_RUN_AUTO_CREATE_BRAND", "Bambu Lab")
)


class _Settings:
    """Lazy settings object that reads from Django settings with defaults."""

    @property
    def TIMEZONE(self):
        return get_setting("BAMBU_RUN_TIMEZONE", "UTC")

    @property
    def BASE_TEMPLATE(self):
        return get_setting("BAMBU_RUN_BASE_TEMPLATE", "bambu_run/base.html")

    @property
    def LOGIN_URL(self):
        return get_setting("BAMBU_RUN_LOGIN_URL", "/accounts/login/")

    @property
    def AUTO_CREATE_BRAND(self):
        return get_setting("BAMBU_RUN_AUTO_CREATE_BRAND", "Bambu Lab")

    # MCP Server settings
    @property
    def MCP_API_KEY(self):
        return get_setting("BAMBU_RUN_MCP_API_KEY", None)

    @property
    def MCP_HOST(self):
        return get_setting("BAMBU_RUN_MCP_HOST", "0.0.0.0")

    @property
    def MCP_PORT(self):
        return get_setting("BAMBU_RUN_MCP_PORT", 8808)

    @property
    def MCP_AUTH_BACKEND(self):
        return get_setting("BAMBU_RUN_MCP_AUTH_BACKEND", None)

    @property
    def MCP_HIDE_SENSITIVE(self):
        return get_setting("BAMBU_RUN_MCP_HIDE_SENSITIVE", False)

    # Cloud sync settings
    @property
    def CLOUD_SYNC_ENABLED(self):
        return get_setting("BAMBU_RUN_CLOUD_SYNC_ENABLED", True)

    @property
    def CLOUD_SYNC_DAYS(self):
        return get_setting("BAMBU_RUN_CLOUD_SYNC_DAYS", 30)

    # Seconds of silence on the MQTT report topic that, once broken by a new
    # message, is treated as "the printer was probably offline" and triggers
    # a pushall re-sync instead of trusting the partial delta to fill in stale
    # fields left over from before the gap.
    @property
    def MQTT_RESYNC_GAP_SECONDS(self):
        return get_setting("BAMBU_RUN_MQTT_RESYNC_GAP_SECONDS", 90)

app_settings = _Settings()
