#------------------------------------------------------------------------------
# written by:   mcdaniel
#               https://lawrencemcdaniel.com
#
# date:         jan-2023 - ensure https protocol
#               apr-2024
#               nov-2024 - added ensure_https, and preserve the original values if they exist
#
# usage:        hard-code protocols to https
# copied from:  https://github.com/overhangio/tutor-mfe/blob/master/tutormfe/patches/openedx-lms-production-settings
#------------------------------------------------------------------------------
# Dynamic config API settings
# https://openedx.github.io/frontend-platform/module-Config.html
def ensure_https(url):
    if url.startswith("http://") and "{{ LMS_HOST }}" in url:
        return url.replace("http://", "https://")
    return url

MFE_CONFIG = MFE_CONFIG or {}
MFE_CONFIG["BASE_URL"] = MFE_CONFIG.get("BASE_URL", "{{ MFE_HOST }}")
MFE_CONFIG["CSRF_TOKEN_API_PATH"] = MFE_CONFIG.get("CSRF_TOKEN_API_PATH", "/csrf/api/v1/token")
MFE_CONFIG["CREDENTIALS_BASE_URL"] = MFE_CONFIG.get("CREDENTIALS_BASE_URL", "")
MFE_CONFIG["DISCOVERY_API_BASE_URL"] = MFE_CONFIG.get("DISCOVERY_API_BASE_URL", "{% if DISCOVERY_HOST is defined %}https://{{ DISCOVERY_HOST }}{% endif %}")
MFE_CONFIG["FAVICON_URL"] = ensure_https(MFE_CONFIG.get("FAVICON_URL", "https://{{ LMS_HOST }}/favicon.ico"))
MFE_CONFIG["INFO_EMAIL"] = MFE_CONFIG.get("INFO_EMAIL", "{{ CONTACT_EMAIL }}")
MFE_CONFIG["LANGUAGE_PREFERENCE_COOKIE_NAME"] = MFE_CONFIG.get("LANGUAGE_PREFERENCE_COOKIE_NAME", "openedx-language-preference")
MFE_CONFIG["LMS_BASE_URL"] = ensure_https(MFE_CONFIG.get("LMS_BASE_URL", "https://{{ LMS_HOST }}"))
MFE_CONFIG["LOGIN_URL"] = ensure_https(MFE_CONFIG.get("LOGIN_URL", "https://{{ LMS_HOST }}/login"))
MFE_CONFIG["LOGO_URL"] = ensure_https(MFE_CONFIG.get("LOGO_URL", "https://{{ LMS_HOST }}/theming/asset/images/logo.png"))
MFE_CONFIG["LOGO_WHITE_URL"] = ensure_https(MFE_CONFIG.get("LOGO_WHITE_URL", "https://{{ LMS_HOST }}/theming/asset/images/logo.png"))
MFE_CONFIG["LOGO_TRADEMARK_URL"] = ensure_https(MFE_CONFIG.get("LOGO_TRADEMARK_URL", "https://{{ LMS_HOST }}/theming/asset/images/logo.png"))
MFE_CONFIG["LOGOUT_URL"] = ensure_https(MFE_CONFIG.get("LOGOUT_URL", "https://{{ LMS_HOST }}/logout"))
MFE_CONFIG["MARKETING_SITE_BASE_URL"] = ensure_https(MFE_CONFIG.get("MARKETING_SITE_BASE_URL", "https://{{ LMS_HOST }}"))
MFE_CONFIG["PASSWORD_RESET_SUPPORT_LINK"] = MFE_CONFIG.get("PASSWORD_RESET_SUPPORT_LINK", "mailto:{{ CONTACT_EMAIL }}")
MFE_CONFIG["REFRESH_ACCESS_TOKEN_ENDPOINT"] = ensure_https(MFE_CONFIG.get("REFRESH_ACCESS_TOKEN_ENDPOINT", "https://{{ LMS_HOST }}/login_refresh"))
MFE_CONFIG["SITE_NAME"] = MFE_CONFIG.get("SITE_NAME", "{{ PLATFORM_NAME }}")
MFE_CONFIG["STUDIO_BASE_URL"] = ensure_https(MFE_CONFIG.get("STUDIO_BASE_URL", "https://{{ CMS_HOST }}"))
MFE_CONFIG["USER_INFO_COOKIE_NAME"] = MFE_CONFIG.get("USER_INFO_COOKIE_NAME", "user-info")
MFE_CONFIG["ACCESS_TOKEN_COOKIE_NAME"] = MFE_CONFIG.get("ACCESS_TOKEN_COOKIE_NAME", "edx-jwt-cookie-header-payload")

# MFE-specific settings

{% if get_mfe("authn") %}
AUTHN_MICROFRONTEND_URL = "https://{{ MFE_HOST }}/authn"
AUTHN_MICROFRONTEND_DOMAIN  = "{{ MFE_HOST }}/authn"
MFE_CONFIG["DISABLE_ENTERPRISE_LOGIN"] = True
{% endif %}

{% if get_mfe("account") %}
ACCOUNT_MICROFRONTEND_URL = "https://{{ MFE_HOST }}/account"
MFE_CONFIG["ACCOUNT_SETTINGS_URL"] = ACCOUNT_MICROFRONTEND_URL
{% endif %}

{% if get_mfe("course-authoring") %}
MFE_CONFIG["ENABLE_NEW_EDITOR_PAGES"] = True
MFE_CONFIG["ENABLE_PROGRESS_GRAPH_SETTINGS"] = True
MFE_CONFIG["COURSE_AUTHORING_MICROFRONTEND_URL"] = "https://{{ MFE_HOST }}/course-authoring"
{% endif %}

{% if get_mfe("discussions") %}
DISCUSSIONS_MICROFRONTEND_URL = "https://{{ MFE_HOST }}/discussions"
MFE_CONFIG["DISCUSSIONS_MFE_BASE_URL"] = DISCUSSIONS_MICROFRONTEND_URL
DISCUSSIONS_MFE_FEEDBACK_URL = None
{% endif %}

{% if get_mfe("gradebook") %}
WRITABLE_GRADEBOOK_URL = "https://{{ MFE_HOST }}/gradebook"
{% endif %}

{% if get_mfe("learner-dashboard") %}
LEARNER_HOME_MICROFRONTEND_URL = "https://{{ MFE_HOST }}/learner-dashboard/"
{% endif %}

{% if get_mfe("learning") %}
LEARNING_MICROFRONTEND_URL = "https://{{ MFE_HOST }}/learning"
MFE_CONFIG["LEARNING_BASE_URL"] = "https://{{ MFE_HOST }}/learning"
{% endif %}

{% if get_mfe("ora-grading") %}
ORA_GRADING_MICROFRONTEND_URL = "https://{{ MFE_HOST }}/ora-grading"
{% endif %}

{% if get_mfe("profile") %}
PROFILE_MICROFRONTEND_URL = "https://{{ MFE_HOST }}/profile/u/"
MFE_CONFIG["ACCOUNT_PROFILE_URL"] = "https://{{ MFE_HOST }}/profile"
{% endif %}

{% if get_mfe("communications") %}
COMMUNICATIONS_MICROFRONTEND_URL = "https://{{ MFE_HOST }}/communications"
MFE_CONFIG["SCHEDULE_EMAIL_SECTION"] = True
{% endif %}

LOGIN_REDIRECT_WHITELIST.append("{{ MFE_HOST }}")
CORS_ORIGIN_WHITELIST.append("https://{{ MFE_HOST }}")
CSRF_TRUSTED_ORIGINS.append("https://{{ MFE_HOST }}")

# CORS and CSRF settings for CDN
CORS_ORIGIN_WHITELIST.append("https://cdn.{{ LMS_HOST }}")
CSRF_TRUSTED_ORIGINS.append("https://cdn.{{ LMS_HOST }}")

{{ patch("mfe-lms-common-settings") }}
{{ patch("mfe-lms-production-settings") }}
