"""
Django settings for project 'my_api.

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

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

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

List of settings that the project as to have to be ready for production:

- General settings:
    - SECRET_KEY: the Django secret key
    - ENABLE_DEBUG: False by default, set to 1 to enable it.
    - ALLOWED_HOSTS: empty by default, use comma-separated list.
    - LOGGING_CONFIG: default None.
    - TIME_ZONE: default 'UTC', list: http://pytz.sourceforge.net/#what-is-utc
    - PATH_TO_STORE_FILE: default BASE_DIR + '/files/'
- DB-related settings:
    - DB_HOST: default 'localhost'
    - DB_USER: default 'user'
    - DB_NAME: default 'project_db'
    - DB_PASS: default 'passwd' in debug mode ssm 'db_password' in production.
    - DB_PORT: default '5432'
- Email settings:
    - alainivars@gmail.com
- Sentry settings:
    - SENTRY_DSN: default is None, set to a valid DSN to enable sentry.
    - SENTRY_RELEASE: default 'local', set to 'stage', 'live', etc.
- AWS settings:
    - TODO
- Silk profiler:
    - ENABLE_SILK: False by default, set to 1 to enable it.

"""

import os
import sys

# import boto3 as boto3
#
# # for the secrets
# aws_client = boto3.client('ssm', region_name='eu-west-1')
# response = aws_client.get_parameters(
#     Names=[
#         'db_password',
#         'email_password',
#         'django_secret_key',
#         'stats_api_key',
#     ],
#     WithDecryption=True
# )
# secrets = {p.get('Name'): p.get('Value') for p in response.get('Parameters')}
secrets = {}

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

PATH_TO_STORE_FILE = os.environ.get(
    'PATH_TO_STORE_FILE', default=BASE_DIR + '/files/')
os.makedirs(PATH_TO_STORE_FILE, exist_ok=True)

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = os.environ.get('SECRET_KEY')
# or with a file
# with open('/etc/secret_key.txt') as f:
#     SECRET_KEY = f.read().strip()

# Are in test mode?
TESTING = 'test' or '-k e2e' in sys.argv

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = bool(int(os.getenv('ENABLE_DEBUG', 0)))
DEBUG_URL = 0
# Allowed hosts
ALLOWED_HOSTS = os.getenv('ALLOWED_HOSTS', '').split(',')
if DEBUG:
    ALLOWED_HOSTS.extend(['127.0.0.1', 'localhost', '0.0.0.0'])

# Email
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'

ADMINS = (
    ('Author', 'alainivars@gmail.com'),
)

REST_FRAMEWORK = {
    'EXCEPTION_HANDLER': 'rest_framework.views.exception_handler',
    'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
    'DEFAULT_SCHEMA_CLASS': 'rest_framework.schemas.coreapi.AutoSchema',
    'PAGE_SIZE': int(os.getenv('DJANGO_PAGINATION_LIMIT', 10)),
    'DATETIME_FORMAT': '%Y-%m-%dT%H:%M:%S%z',

    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework.authentication.BasicAuthentication',  # require for /docs
        'rest_framework.authentication.TokenAuthentication',
    ),
    'DEFAULT_PERMISSION_CLASSES': (
        'rest_framework.permissions.IsAuthenticated',
    ),
    'DEFAULT_RENDERER_CLASSES': (
        'rest_framework.renderers.JSONRenderer',
        'rest_framework.renderers.BrowsableAPIRenderer',
    ),
    'DEFAULT_PARSER_CLASSES': (
        'rest_framework.parsers.JSONParser',
        'rest_framework.parsers.FormParser',
        'rest_framework.parsers.MultiPartParser'
    ),
}

# Application definition
INSTALLED_APPS = [
    # Core authentication framework and its default models.
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    'rest_framework',
    'rest_framework.authtoken',
    'dj_rest_auth',
    'drf_yasg',

    'my_api.rest',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    # Manages sessions across requests
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    # Associates users with requests using sessions.
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'my_api.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 = 'my_api.wsgi.application'

if TESTING:
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.sqlite3',
            'NAME': os.path.join(BASE_DIR, 'tests.sqlite3'),
        }
    }
else:
    if DEBUG:
        if os.getenv('DOCKER_CONTAINER'):
            DB_HOST = 'db'
        else:
            DB_HOST = os.getenv('DB_HOST', '127.0.0.1')
        DB_USER = os.getenv('DB_USER', 'user')
        DB_PASS = os.getenv('DB_PASS', 'passwd')
        DB_NAME = os.getenv('DB_NAME', 'project_db')
        DB_PORT = os.getenv('DB_PORT', '5432')
        # todo: change to postgres later, and refactor this part
        DATABASES = {
            'default': {
                'ENGINE': 'django.db.backends.sqlite3',
                'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
            },
        }
    else:
        if os.getenv('DOCKER_CONTAINER'):
            DB_HOST = 'db'
        else:
            DB_HOST = os.getenv('DB_HOST', '127.0.0.1')
        DB_USER = os.getenv('DB_USER', 'user')
        DB_PASS = secrets.get('db_password', 'passwd')
        DB_NAME = os.getenv('DB_NAME', 'project_db')
        DB_PORT = os.getenv('DB_PORT', '5432')
        DATABASES = {
            'default': {
                'ENGINE': 'django.db.backends.postgresql_psycopg2',
                'HOST': DB_HOST,
                'PORT': DB_PORT,
                'NAME': DB_NAME,
                'USER': DB_USER,
                'PASSWORD': DB_PASS,
            }
        }

# Password validation
# https://docs.djangoproject.com/en/2.1/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
    {   # todo; here for the fanatic pep8 line len max 79, tell me how to solve it???
        '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.1/topics/i18n/
LANGUAGE_CODE = 'en-us'

# list: http://pytz.sourceforge.net/#what-is-utc
TIME_ZONE = os.environ.get('TIME_ZONE', default='UTC')
USE_I18N = True
USE_L10N = True
USE_TZ = True

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

# Some media files if you need it else remove it
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), 'media')
MEDIA_URL = '/media/'

# # Sentry
# SENTRY_DSN = os.getenv('SENTRY_DSN', None)
# if SENTRY_DSN is not None:
#     import raven
#     SENTRY_RELEASE = os.getenv('SENTRY_RELEASE', 'local')
#
#     INSTALLED_APPS.append('raven.contrib.django.raven_compat')
#     RAVEN_CONFIG = {
#         'dsn': SENTRY_DSN,
#         'release': SENTRY_RELEASE,
#     }

# Once you’ve set up HTTPS, enable the following settings.
# Set this to True to avoid transmitting the CSRF cookie over HTTP accidentally.
# usefull for BasicAuthentication and SessionAuthentication
# CSRF_COOKIE_SECURE = True
# SESSION_COOKIE_SECURE = True

# Logging
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'django.server': {
            '()': 'django.utils.log.ServerFormatter',
            'format': '[%(server_time)s] %(message)s',
        },
        'verbose': {
            'format':
                # todo; here for the fanatic pep8 line len max 79, tell me how to solve it???
                '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s'
        },
        'simple': {
            'format': '%(levelname)s %(message)s'
        },
    },
    'filters': {
        'require_debug_true': {
            '()': 'django.utils.log.RequireDebugTrue',
        },
    },
    'handlers': {
        'django.server': {
            'level': 'INFO',
            'class': 'logging.StreamHandler',
            'formatter': 'django.server',
        },
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
            'formatter': 'simple'
        },
        'mail_admins': {
            'level': 'ERROR',
            'class': 'django.utils.log.AdminEmailHandler'
        }
    },
    'loggers': {
        'django': {
            'handlers': ['console'],
            'propagate': True,
        },
        'django.server': {
            'handlers': ['django.server'],
            'level': 'INFO',
            'propagate': False,
        },
        'django.request': {
            'handlers': ['mail_admins', 'console'],
            'level': 'ERROR',
            'propagate': False,
        },
        'django.db.backends': {
            'handlers': ['console'],
            'level': 'INFO'
        },
    }
}
