"""
Django settings for art project.

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

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

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

# Standard Library
import os

# Third-Party Imports
import dj_database_url
from decouple import config, Csv

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

# PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.9/howto/static-files/
STATIC_ROOT = os.path.join(BASE_DIR, "staticfiles")
STATIC_URL = "/static/"

STATICFILES_STORAGE = "whitenoise.django.GzipManifestStaticFilesStorage"

# MEdia files
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
MEDIA_URL = "/media/"

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

# SECURITY WARNING: keep the secret key used in production secret!

SECRET_KEY = config("SECRET_KEY")

DATABASES = {"default": dj_database_url.config()}
# When starting the application, at least in development if the following error occurs
# """django.core.exceptions.ImproperlyConfigured: settings.DATABASES is improperly configured. Please supply the ENGINE
# value. Check settings documentation for more details. """
# Please uncomment the following instead
# DATABASES = {"default": dj_database_url.config(default=config('DATABASE_URL'))}

ALLOWED_HOSTS = config("HOST_IP", cast=Csv())
# Application definition

INSTALLED_APPS = [
    "jet",
    "jet.dashboard",
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "django.contrib.messages",
    "django.contrib.staticfiles",
    "rest_framework",
    "core",
    "api",
    "oauth2_provider",
    "drf_yasg",
    "corsheaders",
    "django_filters",
    "django_q",
]

AUTH_USER_MODEL = "core.User"

MIDDLEWARE = [
    "corsheaders.middleware.CorsMiddleware",
    "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",
    "api.requestMiddleware.RequestMiddleware",
]

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

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

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

LANGUAGE_CODE = "en-us"

TIME_ZONE = "UTC"

USE_I18N = True

USE_L10N = True

USE_TZ = True

JET_SIDE_MENU_COMPACT = True

JET_DEFAULT_THEME = "andela"

REST_FRAMEWORK = {
    "DEFAULT_PAGINATION_CLASS": "api.pagination.PageNumberPagination",
    "DEFAULT_AUTHENTICATION_CLASSES": (
        "api.authentication.FirebaseTokenAuthentication",
        "rest_framework.authentication.BasicAuthentication",
        "rest_framework.authentication.SessionAuthentication",
        "oauth2_provider.contrib.rest_framework.OAuth2Authentication",
    ),
    "DEFAULT_FILTER_BACKENDS": ("django_filters.rest_framework.DjangoFilterBackend",),
}

OAUTH2_PROVIDER_APPLICATION_MODEL = "core.APIUser"

LOGGING = {
    "version": 1,
    "disable_existing_loggers": False,
    "formatters": {
        "verbose": {"format": "{levelname} {asctime} {module} {message}", "style": "{"},
        "simple": {"format": "{levelname} {message}", "style": "{"},
    },
    "handlers": {
        "console": {
            "level": config("LOGLEVEL", "info").upper(),
            "class": "logging.StreamHandler",
            "formatter": "verbose",
        },
        "mail_admins": {
            "level": "ERROR",
            "class": "django.utils.log.AdminEmailHandler",
            "formatter": "verbose",
        },
    },
    "loggers": {"": {"handlers": ["console", "mail_admins"], "propagate": True}},
}

csv = Csv(cast=lambda s: tuple(s.split(":")))

ADMINS = csv(config("ADMINS", "art:art.andela@andela.com,art_group:art@andela.com"))

SWAGGER_SETTINGS = {
    "SECURITY_DEFINITIONS": {
        "api_key": {"type": "apiKey", "in": "header", "name": "Authorization"}
    }
}

REDOC_SETTINGS = {"LAZY_RENDERING": True}

# EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend"

EMAIL_HOST = config("EMAIL_HOST", None)
EMAIL_PORT = config("EMAIL_PORT", None)
EMAIL_HOST_USER = config("EMAIL_HOST_USER", None)
EMAIL_HOST_PASSWORD = config("EMAIL_HOST_PASSWORD", None)
EMAIL_USE_TLS = config("EMAIL_USE_TLS", True)

Q_CLUSTER = {
    'name': 'art-backend',
    'timeout': 60,  # The number of seconds a worker is allowed to spend on a task before it’s terminated.
    'queue_limit': 500,
    'cpu_affinity': 1,  # Sets the number of processor each worker can use
    'label': 'ART',
    'orm': 'default',  # Use Django’s database backend as a message broker
}
# configure Django to store its cached data in the database.
# useful for monitoring django_q
CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.db.DatabaseCache',
        'LOCATION': 'art_cache',
    }
}
# log emails in console rather than sending them
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
