"""
Django settings for credo_classification project.

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

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

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

import os

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


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

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = os.environ.get('CREDO_SECRET', 'xxx')

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = os.environ.get('CREDO_DEBUG', '0') == '1'

SECURE_HSTS_SECONDS = 0 if DEBUG else 60*60*24
SECURE_HSTS_INCLUDE_SUBDOMAINS = True
SECURE_SSL_REDIRECT = not DEBUG
SESSION_COOKIE_SECURE = True
CSRF_COOKIE_SECURE = True
SECURE_HSTS_PRELOAD = True
SECURE_REFERRER_POLICY = 'origin'

ALLOWED_HOSTS = ['*']


# Application definition

INSTALLED_APPS = [
    # Django
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    # Django REST framework
    'rest_framework',
    'rest_framework_filters',

    # Project apps
    'users',
    'definitions',
    'database',
    'values',
    'classify'
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'whitenoise.middleware.WhiteNoiseMiddleware',
    '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',
]

STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
ROOT_URLCONF = 'credo_classification.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        '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 = 'credo_classification.wsgi.application'


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

DATABASES = {
    'default': {
        'ENGINE': os.environ.get('CREDO_DB_ENGINE', 'django.db.backends.postgresql'),
        'NAME': os.environ.get('CREDO_DB_NAME', 'credo'),
        'USER': os.environ.get('CREDO_DB_USER', 'credo'),
        'PASSWORD': os.environ.get('CREDO_DB_PASSWORD', 'credo'),
        'HOST': os.environ.get('CREDO_DB_HOST', '127.0.0.1'),
        'PORT': os.environ.get('CREDO_DB_PORT', '5432'),
        'ATOMIC_REQUESTS': True,
    }
}


# Password validation
# https://docs.djangoproject.com/en/3.0/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',
    },
]

AUTHENTICATION_BACKENDS = [
    'users.backends.CredoUserBackend',
]

CREDO_SERVER = os.environ.get('CREDO_SERVER', 'https://api.credo.science/')
APP_SITE = os.environ.get('CREDO_DEPLOY_SITE', 'test')
APP_NAME = 'CREDO Classify'
APP_VERSION = '0.1-alpha'
BASE_URL = os.environ.get('CREDO_ENDPOINT', 'user-interface/classification/')

# Django REST Framework

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': [
        'credo_classification.auth.MyTokenAuthentication',
    ],
    'DEFAULT_FILTER_BACKENDS': [
        'rest_framework_filters_nkg.backends.ComplexFilterNkgBackend',
        'rest_framework.filters.OrderingFilter',
        'rest_framework.filters.SearchFilter'
    ],
    'EXCEPTION_HANDLER': 'credo_classification.drf.api_500_handler'
}


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

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

AUTH_USER_MODEL = 'users.User'

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

STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/%sstatic/' % BASE_URL
STATICFILES_DIRS = [os.path.join(BASE_DIR, "frontend", "build", "static")]
