import os


def _get_env_production_mode():
    env_var = os.environ.get("DJANGO_PRODUCTION_MODE", "false")
    return env_var.lower() == "true"


def _get_env_secret_key():
    env_var = os.environ.get("DJANGO_SECRET_KEY")
    assert env_var is not None, "DJANGO_SECRET_KEY environment variable must be set when using production mode"
    return env_var


def _get_env_allowed_hosts():
    env_var = os.environ.get("DJANGO_ALLOWED_HOSTS")
    assert env_var is not None, "DJANGO_ALLOWED_HOSTS environment variable must be set when using production mode"
    return [host.strip() for host in env_var.split(",")]


def _get_env_static_root():
    env_var = os.environ.get("DJANGO_STATIC_ROOT")
    assert env_var is not None, "DJANGO_STATIC_ROOT environment variable must be set when using production mode"
    return env_var


# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

PRODUCTION = _get_env_production_mode()

if PRODUCTION:
    SECRET_KEY = _get_env_secret_key()
    DEBUG = False
    ALLOWED_HOSTS = _get_env_allowed_hosts()
    CSRF_TRUSTED_ORIGINS = [f"https://{host}" for host in ALLOWED_HOSTS]
    STATIC_ROOT = _get_env_static_root()
else:
    SECRET_KEY = "LqKSgoFtED4IFYxf01lBi5MEI4ExSayCakwLjyuzytDJ7vuMq9"
    DEBUG = True
    ALLOWED_HOSTS = ["*"]

INSTALLED_APPS = [
    "gaehsnitz",
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.messages",
    "django.contrib.sessions",
    "django.contrib.staticfiles",
]

MIDDLEWARE = [
    "django.middleware.security.SecurityMiddleware",
    "django.middleware.common.CommonMiddleware",
    "django.middleware.clickjacking.XFrameOptionsMiddleware",
    "django.middleware.csrf.CsrfViewMiddleware",
    "django.contrib.sessions.middleware.SessionMiddleware",
    "django.contrib.auth.middleware.AuthenticationMiddleware",
    "django.contrib.messages.middleware.MessageMiddleware",
]

DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.postgresql",
        "HOST": os.environ["DB_HOST"],
        "PORT": os.environ["DB_PORT"],
        "NAME": os.environ["DB_NAME"],
        "USER": os.environ["DB_USER"],
        "PASSWORD": os.environ["DB_PASSWORD"],
    }
}

DEFAULT_AUTO_FIELD = "django.db.models.SmallAutoField"

ROOT_URLCONF = "gaehsnitzproject.urls"

TEMPLATES = [
    {
        "BACKEND": "django.template.backends.django.DjangoTemplates",
        "DIRS": [],
        "APP_DIRS": True,
        "OPTIONS": {
            "context_processors": [
                "django.template.context_processors.debug",
                "django.template.context_processors.request",
                "django.contrib.auth.context_processors.auth",
                "django.contrib.messages.context_processors.messages",
            ],
        },
    },
]

WSGI_APPLICATION = "gaehsnitzproject.wsgi.application"

TIME_ZONE = "Europe/Berlin"
LANGUAGE_CODE = "de"
USE_I18N = True
USE_L10N = False
USE_TZ = True

STATIC_URL = "/static/"

AUTH_USER_MODEL = "gaehsnitz.User"

AUTHENTICATION_BACKENDS = [
    "django.contrib.auth.backends.ModelBackend",
    "gaehsnitz.auth_backends.PinBackend",
]

LOGIN_URL = "/suff/"
