"""
Django settings for leaky project.

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

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, sys

# 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 = '2wrpt_gf1-2rjf2f=3=&0mlz-m_^7n&s#qwh4$c8*3q&wb+d4o'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = int(os.environ.setdefault("LEAKY_DEBUG", 0))
TENANT_USERS_DOMAIN = os.environ.setdefault("LEAKY_DOMAIN_NAME", "leaky.com")
SESSION_COOKIE_DOMAIN = ".{}".format(TENANT_USERS_DOMAIN)
SYSTEM_EMAIL = os.environ.setdefault("LEAKY_SYSTEM_EMAIL", "root@leaky.com")
ADMIN_EMAIL = os.environ.setdefault("LEAKY_ADMIN_EMAIL", "admin@leaky.com")
ADMIN_PASSWORD = os.environ.setdefault("LEAKY_ADMIN_PASSWORD", "password")
ALLOWED_HOSTS = [SESSION_COOKIE_DOMAIN]

# Tenant=Store, TennatGroup=Corporation
AUTO_CREATE_TEST_TENANT_GROUP = int(os.environ.setdefault("LEAKY_CREATE_TEST_TENANT_GROUP", DEBUG and "1" or "0"))
AUTO_CREATE_TEST_TENANT = int(os.environ.setdefault("LEAKY_CREATE_TEST_TENANT", DEBUG and "1" or "0"))
TEST_TENANT_GROUP_NAME = "test_corporation"
TEST_TENANT_HOST = "test"
TEST_TENANT_NAME = "Test Store"
LEAKY_VERSION = "v0.0.1"

PUBLIC_SCHEMA_URLCONF = 'leaky.urls_public'
ROOT_URLCONF = 'leaky.urls_tenants'
TENANT_MODEL = "customers.Store"  # app.Model
AUTH_USER_MODEL = "customers.StoreUser"

MIDDLEWARE = [
    'tenant_schemas.middleware.TenantMiddleware',
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'graphql_jwt.middleware.JWTMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

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',
            ],
        },
    },
]

SHARED_APPS = (
    'tenant_schemas',  # mandatory, should always be before any django app

    'django.contrib.auth',
    'django.contrib.contenttypes',
    'tenant_users.permissions',
    'tenant_users.tenants',
    'customers',

    # everything below here is optional
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.admin',
)


TENANT_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'tenant_users.permissions',

    'warehouses',
    #'graphene_django',
)

INSTALLED_APPS = (
    'tenant_schemas',  # mandatory, should always be before any django app

    'django.contrib.auth',
    'django.contrib.contenttypes',
    'tenant_users.permissions',
    'tenant_users.tenants',
    'channels',
    'channels_api',

    'customers',
    'warehouses',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.admin',
    'graphene_django',
    'djmoney',
)

CHANNEL_LAYERS = {
  "default": {
    "BACKEND": "asgiref.inmemory.ChannelLayer",
    "ROUTING": "leaky.routing.project_routing",  # Our project routing
  },
}

GRAPHENE = {
    'SCHEMA': 'leaky.schema.public_schema',
    'RELAY_CONNECTION_MAX_LIMIT': sys.maxsize,  # we can set the 'max_limit' kwarg on your DjangoConnectionField too
    'RELAY_CONNECTION_ENFORCE_FIRST_OR_LAST': True,
    'MIDDLEWARE': [
        'graphene_django.debug.DjangoDebugMiddleware',
    ]
}


WSGI_APPLICATION = 'leaky.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.11/ref/settings/#databases
DATABASES = {
    'default': {
        'ENGINE': 'tenant_schemas.postgresql_backend',
        'NAME': os.environ.get("LEAKY_DATABASE_NAME"),
        'USER': os.environ.get("LEAKY_DATABASE_USER"),
        'PASSWORD': os.environ.get("LEAKY_DATABASE_PASSWORD"),
        'HOST': os.environ.get("LEAKY_DATABASE_HOST"),
        'PORT': os.environ.get("LEAKY_DATABASE_PORT"),
        'CONN_MAX_AGE': 500,
    }
}
DATABASE_ROUTERS = (
    'tenant_schemas.routers.TenantSyncRouter',
)

AUTHENTICATION_BACKENDS = (
    'graphql_jwt.backends.JWTBackend',
    'tenant_users.permissions.backend.UserBackend',
)

# 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/'

DEFAULT_FILE_STORAGE = 'tenant_schemas.storage.TenantFileSystemStorage'
