# Dynamic config API settings
# https://openedx.github.io/frontend-platform/module-Config.html
MFE_CONFIG = {
    "BASE_URL": "{{ MFE_HOST }}",
    "CSRF_TOKEN_API_PATH": "/csrf/api/v1/token",
    "CREDENTIALS_BASE_URL": "",
    "DISCOVERY_API_BASE_URL": "{% if DISCOVERY_HOST is defined %}http://{{ DISCOVERY_HOST }}:8381{% endif %}",
    "FAVICON_URL": "http://{{ LMS_HOST }}/favicon.ico",
    "INFO_EMAIL": "{{ CONTACT_EMAIL }}",
    "LANGUAGE_PREFERENCE_COOKIE_NAME": "openedx-language-preference",
    "LMS_BASE_URL": "http://{{ LMS_HOST }}:8000",
    "LOGIN_URL": "http://{{ LMS_HOST }}:8000/login",
    "LOGO_URL": "http://{{ LMS_HOST }}:8000/theming/asset/images/logo.png",
    "LOGO_WHITE_URL": "http://{{ LMS_HOST }}:8000/theming/asset/images/logo.png",
    "LOGO_TRADEMARK_URL": "http://{{ LMS_HOST }}:8000/theming/asset/images/logo.png",
    "LOGOUT_URL": "http://{{ LMS_HOST }}:8000/logout",
    "MARKETING_SITE_BASE_URL": "http://{{ LMS_HOST }}:8000",
    "PASSWORD_RESET_SUPPORT_LINK": "mailto:{{ CONTACT_EMAIL }}",
    "REFRESH_ACCESS_TOKEN_ENDPOINT": "http://{{ LMS_HOST }}:8000/login_refresh",
    "SITE_NAME": "{{ PLATFORM_NAME }}",
    "STUDIO_BASE_URL": "http://{{ CMS_HOST }}:8001",
    "USER_INFO_COOKIE_NAME": "user-info",
    "ACCESS_TOKEN_COOKIE_NAME": "edx-jwt-cookie-header-payload",
}

FRONTEND_SITE_CONFIG = {
    "baseUrl": "http://{{ MFE_HOST }}:{{ MFE_SITE_PORT }}",
    "cmsBaseUrl": "http://{{ CMS_HOST }}:8001",
    "lmsBaseUrl": "http://{{ LMS_HOST }}:8000",
    "loginUrl": "http://{{ LMS_HOST }}:8000/login",
    "logoutUrl": "http://{{ LMS_HOST }}:8000/logout",
    "externalRoutes": [
        {"role": "org.openedx.frontend.role.logout", "url": "http://{{ LMS_HOST }}:8000/logout"},
    ],
    "commonAppConfig": {},
}

# MFE-specific settings
{% if get_mfe("authn") or is_frontend_app_enabled("authn") %}
AUTHN_MICROFRONTEND_DOMAIN  = "{{ MFE_HOST }}/authn"
MFE_CONFIG["DISABLE_ENTERPRISE_LOGIN"] = True
FRONTEND_SITE_CONFIG["commonAppConfig"]["DISABLE_ENTERPRISE_LOGIN"] = True
{% if is_frontend_app_enabled("authn") %}
AUTHN_MICROFRONTEND_URL = "http://{{ MFE_HOST }}:{{ MFE_SITE_PORT }}/authn"
{% else %}
AUTHN_MICROFRONTEND_URL = "http://{{ MFE_HOST }}:{{ get_mfe("authn")["port"] }}/authn"
{% endif %}
{% endif %}

{% if get_mfe("account") %}
ACCOUNT_MICROFRONTEND_URL = "http://{{ MFE_HOST }}:{{ get_mfe("account")["port"] }}/account/"
MFE_CONFIG["ACCOUNT_SETTINGS_URL"] = ACCOUNT_MICROFRONTEND_URL
FRONTEND_SITE_CONFIG["externalRoutes"].append({"role": "org.openedx.frontend.role.account", "url": "http://{{ MFE_HOST }}:{{ get_mfe("account")["port"] }}/account/"})
{% endif %}

{% if get_mfe("authoring") %}
COURSE_AUTHORING_MICROFRONTEND_URL = "http://{{ MFE_HOST }}:{{ get_mfe("authoring")["port"] }}/authoring"
MFE_CONFIG["COURSE_AUTHORING_MICROFRONTEND_URL"] = COURSE_AUTHORING_MICROFRONTEND_URL
MFE_CONFIG["ENABLE_ASSETS_PAGE"] = "true"
MFE_CONFIG["ENABLE_HOME_PAGE_COURSE_API_V2"] = "true"
MFE_CONFIG["ENABLE_PROGRESS_GRAPH_SETTINGS"] = "true"
MFE_CONFIG["ENABLE_TAGGING_TAXONOMY_PAGES"] = "true"
MFE_CONFIG["ENABLE_UNIT_PAGE"] = "true"
MFE_CONFIG["MEILISEARCH_ENABLED"] = "true"
{% endif %}

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

{% if get_mfe("gradebook") %}
WRITABLE_GRADEBOOK_URL = "http://{{ MFE_HOST }}:{{ get_mfe("gradebook")["port"] }}/gradebook"
{% endif %}

{% if is_frontend_app_enabled("learner-dashboard") %}
LEARNER_HOME_MICROFRONTEND_URL = "http://{{ MFE_HOST }}:{{ MFE_SITE_PORT }}/learner-dashboard/"
{% elif get_mfe("learner-dashboard") %}
LEARNER_HOME_MICROFRONTEND_URL = "http://{{ MFE_HOST }}:{{ get_mfe("learner-dashboard")["port"] }}/learner-dashboard/"
{% endif %}

{% if is_frontend_app_enabled("instructor-dashboard") %}
INSTRUCTOR_MICROFRONTEND_URL = "http://{{ MFE_HOST }}:{{ MFE_SITE_PORT }}/instructor-dashboard"
{% endif %}

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

{% if get_mfe("ora-grading") %}
ORA_GRADING_MICROFRONTEND_URL = "http://{{ MFE_HOST }}:{{ get_mfe("ora-grading")["port"] }}/ora-grading"
{% endif %}

{% if get_mfe("profile") %}
PROFILE_MICROFRONTEND_URL = "http://{{ MFE_HOST }}:{{ get_mfe("profile")["port"] }}/profile/u/"
MFE_CONFIG["ACCOUNT_PROFILE_URL"] = "http://{{ MFE_HOST }}:{{ get_mfe("profile")["port"] }}/profile"
FRONTEND_SITE_CONFIG["externalRoutes"].append({"role": "org.openedx.frontend.role.profile", "url": "http://{{ MFE_HOST }}:{{ get_mfe("profile")["port"] }}/profile/"})
{% endif %}

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

{% if get_mfe("admin-console") %}
ADMIN_CONSOLE_MICROFRONTEND_URL = "http://{{ MFE_HOST }}:{{ get_mfe("admin-console")["port"] }}/admin-console"
MFE_CONFIG["ADMIN_CONSOLE_URL"] = ADMIN_CONSOLE_MICROFRONTEND_URL
{% endif %}

{% if get_mfe("catalog") %}
CATALOG_MICROFRONTEND_URL = "http://{{ MFE_HOST }}:{{ get_mfe("catalog")["port"] }}/catalog"
{% endif %}

{% if is_frontend_app_enabled("notifications") %}
NOTIFICATIONS_DEFAULT_FROM_EMAIL = ENV_TOKENS.get("NOTIFICATIONS_DEFAULT_FROM_EMAIL", ENV_TOKENS["CONTACT_EMAIL"])
{% endif %}

# Cors configuration
{% for app_name, app in iter_mfes() %}
# {{ app_name }} MFE
CORS_ORIGIN_WHITELIST.append("http://{{ MFE_HOST }}:{{ app["port"] }}")
LOGIN_REDIRECT_WHITELIST.append("{{ MFE_HOST }}:{{ app["port"] }}")
CSRF_TRUSTED_ORIGINS.append("http://{{ MFE_HOST }}:{{ app["port"] }}")
{% endfor %}

{% if get_frontend_apps() %}
CORS_ORIGIN_WHITELIST.append("http://{{ MFE_HOST }}:{{ MFE_SITE_PORT }}")
LOGIN_REDIRECT_WHITELIST.append("{{ MFE_HOST }}:{{ MFE_SITE_PORT }}")
CSRF_TRUSTED_ORIGINS.append("http://{{ MFE_HOST }}:{{ MFE_SITE_PORT }}")
{% endif %}

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