"""
SDC Django settings extension for §PROJECT§.

Generated by '§PROJECT§' using SDC.

For more information on this file, see
https://simpledomcontrol.readthedocs.io/en/latest/
"""
import os
from urllib.parse import urlparse, urlunparse

from §PROJECT§.base_settings import *


if not DEBUG:
    hosts = [urlparse(x) for x in os.environ.get('ALLOWED_HOST').split(',')]
    ALLOWED_HOSTS = [host.hostname for host in hosts]
    CSRF_TRUSTED_ORIGINS = [urlunparse(x) for x in hosts]
else:
    ALLOWED_HOSTS = ['*']

if 'VERSION' not in locals():
    VERSION = 0.0

# Add daphne amd SDC packages to installed apps
# (https://docs.djangoproject.com/en/5.1/howto/deployment/asgi/daphne/)

INSTALLED_APPS = ['daphne'] + INSTALLED_APPS + ['channels', 'sdc_tools', 'sdc_user']

if 'INTERNAL_IPS' not in locals():
    INTERNAL_IPS = (
        '127.0.0.1',
    )

# DEFAULT_AUTO_FIELD = 'django.db.models.AutoField'

STATIC_ROOT = BASE_DIR / 'www/'

STATICFILES_DIRS = [BASE_DIR / "static", BASE_DIR / "node_modules"]

# Set ASGI root (https://docs.djangoproject.com/en/5.1/howto/deployment/asgi/daphne/)

ASGI_APPLICATION = '§PROJECT§.asgi.application'

# Add jest database for SDC tests

DATABASES_AVAILABLE = {
                          'jest': {'ENGINE': 'django.db.backends.sqlite3', 'NAME': BASE_DIR / 'test_db.sqlite3', },
                      } | DATABASES

database = os.environ.get('DJANGO_DATABASE', 'default')

DATABASES = {'default': DATABASES_AVAILABLE[database]}

# Setup necessary template settings for SDC

TEMPLATES[0]["APP_DIRS"] = True
if BASE_DIR / "templates" not in TEMPLATES[0]["DIRS"]:
    TEMPLATES[0]["DIRS"].append(BASE_DIR / "templates")

if DEBUG:
    CHANNEL_LAYERS = {
        "default": {
            "BACKEND": "channels.layers.InMemoryChannelLayer"
        }
    }
else:
    CHANNEL_LAYERS = {
        'default': {
            'BACKEND': 'channels_redis.core.RedisChannelLayer',
            'CONFIG': {
                "hosts": [('redis', 6379)],
            },
        },
    }

if 'MEDIA_URL' not in locals():
    MEDIA_URL = '/media/'

if 'MEDIA_ROOT' not in locals():
    MEDIA_ROOT = BASE_DIR / 'media'

SERVER_CALL_VIA_WEB_SOCKET = False
MODEL_FORM_TEMPLATE = "elements/form.html"
LOGIN_CONTROLLER = 'sdc-login'
LOGIN_SUCCESS = '/'

# EMAIL_BACKEND='django.core.mail.backends.smtp.EmailBackend'
# EMAIL_HOST =''
# EMAIL_PORT = 587
# EMAIL_HOST_USER = ''
# DEFAULT_FROM_EMAIL = ''
# EMAIL_HOST_PASSWORD = ''
# EMAIL_USE_TLS = True
