"""
Django settings for django_vue_multitenant project.

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

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

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

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
from django.core.exceptions import ImproperlyConfigured

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
DEBUG = True

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

# Normally you should not import ANYTHING from Django directly
# into your settings, but ImproperlyConfigured is an exception.
# JSON-based secrets module
with open(os.path.join(BASE_DIR, 'settings/secrets.json')) as f:
    secrets = json.loads(f.read())


def get_secret(setting, secrets=secrets):
    """Get the secret variable or return explicit exception."""
    try:
        return secrets[setting]
    except KeyError:
        error_msg = "Set the {0} environment variable".format(setting)
        raise ImproperlyConfigured(error_msg)


# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = get_secret("SECRET_KEY")

TEMPLATES_DIR = os.path.join(BASE_DIR, '..', 'templates')
# FRONTEND_DIR = os.path.join(BASE_DIR, '..', 'frontend')

# WEBPACK_LOADER = {
#     'DEFAULT': {
#         'CACHE': True,
#         'BUNDLE_DIR_NAME': '/bundles/',  # must end with slash
#         'STATS_FILE': os.path.join(FRONTEND_DIR, 'webpack-stats.json'),
#     }
# }
VUE_FRONTEND_DIR = os.path.join(BASE_DIR,'..','frontend')
WEBPACK_LOADER = {
    'DEFAULT': {
        'CACHE': not DEBUG,
        'BUNDLE_DIR_NAME': 'vue/',  # must end with slash
        'STATS_FILE': os.path.join(VUE_FRONTEND_DIR, 'webpack-stats.json'),
        'POLL_INTERVAL': 0.1,
        'TIMEOUT': None,
        'IGNORE': [r'.+\.hot-update.js', r'.+\.map']
    }
}

MIDDLEWARE = [
    'plans.plan_middleware.VerifyActivePlan',
    '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',
]

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [TEMPLATES_DIR, ],
        '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 = 'config.wsgi.application'


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

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, '..', 'static_collected')
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, '..', 'media')


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

TENANT_MODEL = "tenants.Pizzeria"
TENANT_DOMAIN_MODEL = "tenants.Domain"
ROOT_URLCONF = 'config.tenant_urls'
PUBLIC_SCHEMA_URLCONF = 'config.public_urls'

SITE_ID = 1
PRODUCT_NAME = "SUPER PIZZAS"

AUTH_USER_MODEL = 'users.UserProfile'

DATABASE_ROUTERS = (
    'django_tenants.routers.TenantSyncRouter',
)

LOGIN_REDIRECT_URL = '/admin'
LOGOUT_REDIRECT_URL = '/'


REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework.authentication.TokenAuthentication',
        'rest_framework.authentication.SessionAuthentication',
    )
}

AUTHENTICATION_BACKENDS = [
    'social_core.backends.google.GoogleOAuth2',
    'social_core.backends.facebook.FacebookOAuth2',
    'django.contrib.auth.backends.ModelBackend',
    # 'django.contrib.auth.backends.RemoteUserBackend'
]

SOCIAL_AUTH_FACEBOOK_KEY = 541574826607624  # App ID
SOCIAL_AUTH_FACEBOOK_SECRET = "3210459decc1511fd82b586503a3c304"
SOCIAL_AUTH_REDIRECT_IS_HTTPS = True
SOCIAL_AUTH_GOOGLE_OAUTH2_KEY = '874956280989-7gfq1jtefll2tp3b0l0mfailft520bda.apps.googleusercontent.com'
SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET = 'NxZAe9VxWFdRNiiPxzXYqBv0'

RECAPTCHA_PRIVATE_KEY = '6LdJRsYUAAAAAL0F31JROVQYd63M_z42YHQbSqWL'
RECAPTCHA_PUBLIC_KEY = '6LdJRsYUAAAAAGFjEexw6dm-PrJ1nGb4zv3FQ1b6'

MAX_PRODUCT_CREATE = 5
MAX_INGREDIENTS_BY_PRODUCTS = 3
