"""
Django settings for forager_backend project.

Generated by 'django-admin startproject' using Django 3.1.1.

For more information on this file, see
https://docs.djangoproject.com/en/3.1/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.1/ref/settings/
"""

import json
import os
import os.path
import tempfile
from pathlib import Path

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.1/howto/deployment/checklist/

django_settings_path = os.path.expanduser("~/.forager/django_settings.json")
if os.path.exists(django_settings_path):
    with open(django_settings_path, "r") as f:
        django_settings = json.load(f)
else:
    temp_dir = tempfile.TemporaryDirectory()
    django_settings = {
        "temp_dir": temp_dir,
        "secret_key": "s&*+2lskkfm0l&ni9rd873xhy3tdb_04*w3cpon9*)1m8ehtib",
        "frontend_port": 4000,
        "allowed_hosts": [],
        "db": {
            "ENGINE": "django.db.backends.sqlite3",
            "NAME": temp_dir.name + "/db.sqlite3",
        },
    }

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = django_settings["secret_key"]

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = django_settings["allowed_hosts"]


# Application definition
INSTALLED_APPS = [
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "django.contrib.messages",
    "django.contrib.staticfiles",
    "corsheaders",
    "rest_framework",
    "sslserver",
    "forager_backend_api.apps.ForagerServerApiConfig",
]

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

ROOT_URLCONF = "forager_backend.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 = "forager_backend.wsgi.application"


# Database
# https://docs.djangoproject.com/en/3.1/ref/settings/#databases

DATABASES = {"default": django_settings["db"]}


# Password validation
# https://docs.djangoproject.com/en/3.1/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        "NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator",
    },
    {
        "NAME": "django.contrib.auth.password_validation.MinimumLengthValidator",
    },
    {
        "NAME": "django.contrib.auth.password_validation.CommonPasswordValidator",
    },
    {
        "NAME": "django.contrib.auth.password_validation.NumericPasswordValidator",
    },
]

# Logging

LOGGING = {
    "version": 1,
    "disable_existing_loggers": False,
    "formatters": {
        "simple": {
            "format": "{asctime} - {name} - {levelname} - {message}",
            "style": "{",
        },
    },
    "handlers": {},
    "root": {"handlers": [], "level": "DEBUG"},
    "loggers": {
        "django": {"handlers": [], "propagate": True},
        "forager_backend": {"handlers": [], "propagate": True},
    },
}

if os.environ.get("FORAGER_LOG_DIR"):
    path = os.environ["FORAGER_LOG_DIR"]
    LOGGING["handlers"]["file"] = {
        "class": "logging.FileHandler",
        "filename": os.path.join(path, "web_server.log"),
        "level": "DEBUG",
        "formatter": "simple",
    }
    LOGGING["root"]["handlers"].append("file")

if os.environ.get("FORAGER_LOG_CONSOLE") == "1":
    LOGGING["handers"]["console"] = {
        "class": "logging.StreamHandler",
        "level": "DEBUG",
        "formatter": "simple",
    }
    LOGGING["root"]["handlers"].append("console")


# Internationalization
# https://docs.djangoproject.com/en/3.1/topics/i18n/

LANGUAGE_CODE = "en-us"

TIME_ZONE = "UTC"

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.1/howto/static-files/

STATIC_URL = "/static/"

# source of the frontend requests
frontend_port = django_settings.get("frontend_port", 4000)
CORS_ALLOW_CREDENTIALS = True
CORS_ORIGIN_WHITELIST = [
    "http://" + h + ((":" + str(frontend_port)) if frontend_port != 80 else "")
    for h in django_settings["allowed_hosts"] + ["127.0.0.1", "localhost"]
]
CSRF_TRUSTED_ORIGINS = ["127.0.0.1", "localhost"] + ALLOWED_HOSTS
SESSION_COOKIE_SAMESITE = "None"  # as a string
SESSION_COOKIE_SECURE = True


EMBEDDING_SERVER_ADDRESS = "http://0.0.0.0:5000"
EMBEDDING_CLUSTER_NODES = 35

SVM_NUM_NEGS_MULTIPLIER = 7
BGSPLIT_NUM_NEGS_MULTIPLIER = 10
