"""
Django settings for abi_back project.

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

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

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.11/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/1.11/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '49x7rj859gjcz(a*dj!75+8lrusnd3(_th@_w!b*3dzu5=&z-q'

# SECURITY WARNING: don't run with debug turned on in production!
ENV = os.environ.get('ENV', "DEVELOPMENT")
DEBUG = os.environ.get('DEBUG', "False") == "True"
ABI_REGISTER_ROUTE_53 = os.environ.get('ABI_REGISTER_ROUTE_53', "False") == "True"

MAIN_HOST = os.environ.get("MAIN_HOST", "localhost")

ALLOWED_HOSTS = [
    MAIN_HOST,
    ".{}".format(MAIN_HOST)
    # ".localhost"

]


# Application definition

# We must split Shared apps and Tenant apps to make django app complaint with django tenants
# http://django-tenants.readthedocs.io/en/latest/install.html#configure-tenant-and-shared-applications
SHARED_APPS = [
    # Django tenants must be first line on the Shared apps Iterable
    'django_tenants',
    # After we must have the tenant configuration app
    'abi_back.tenant_configuration',

    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    # Contraslash Apps
    'base',
    'applications.authentication',

    # Custom apps
    'applications.core',

    # Third party apps
    'raven.contrib.django.raven_compat',
    'storages',
    'rest_framework'
]

TENANT_APPS = [
    # Consider to delete from here
    'django.contrib.admin',
    'django.contrib.contenttypes',
    'django.contrib.auth',
    'django.contrib.messages',
    'abi_back.tenant_configuration.custom_dts_test_app',

    # Contraslash Apps
    'applications.authentication',

    # Custom apps

    'applications.core',
    'applications.appointments',
]

INSTALLED_APPS = list(set(SHARED_APPS + TENANT_APPS))

# We also define the Tenant model and Domain model
TENANT_MODEL = 'tenant_configuration.Tenant'  # app.Model

TENANT_DOMAIN_MODEL = 'tenant_configuration.Domain'  # app.Model

MIDDLEWARE = [
    'django_tenants.middleware.main.TenantMainMiddleware',
    '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',
]

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


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

POSTGRESQL_DATABASE_NAME = os.environ.get("ABI_DATABASE_DATABASE", "")
POSTGRESQL_DATABASE_USERNAME = os.environ.get("ABI_DATABASE_USERNAME", "")
POSTGRESQL_DATABASE_PASSWORD = os.environ.get("ABI_DATABASE_PASSWORD", "")
POSTGRESQL_DATABASE_HOST = os.environ.get("ABI_DATABASE_HOST", "localhost")
POSTGRESQL_DATABASE_PORT = os.environ.get("ABI_DATABASE_PORT", "5432")

# We must use django_tenants_postgresql_backend as DATABASE BACKEND
# http://django-tenants.readthedocs.io/en/latest/install.html#basic-settings
DATABASES = {
    'default': {
        'ENGINE': 'django_tenants.postgresql_backend',
        'NAME': POSTGRESQL_DATABASE_NAME,
        'USER': POSTGRESQL_DATABASE_USERNAME,
        'PASSWORD': POSTGRESQL_DATABASE_PASSWORD,
        'HOST': POSTGRESQL_DATABASE_HOST,
        'PORT': POSTGRESQL_DATABASE_PORT,
    }
}

# And also define django_tenants.routers.TenantSyncRouter as DATABASE_ROUTERS

DATABASE_ROUTERS = [
    'django_tenants.routers.TenantSyncRouter',
]

# This is default but we want to have explicit declaration of public schema name
# http://django-tenants.readthedocs.io/en/latest/install.html#optional-settings
PUBLIC_SCHEMA_NAME = 'public'


# And we aso want to define a specific set of routes to manage tenant_configuration
PUBLIC_SCHEMA_URLCONF = 'abi_back.tenant_configuration.urls'

# Password validation
# https://docs.djangoproject.com/en/1.11/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/1.11/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/1.11/howto/static-files/

STATIC_URL = '/static/'

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "static"),
]


# AUTH Configuration
AUTH_TEMPLATE_FOLDER = "custom_authentication"
LOGIN_URL = "log_in"
LOGIN_REDIRECT_URL = "/"

if ENV == "STAGING":
    from abi_back.settings_staging import *
