import os

import sentry_sdk
from sentry_sdk.integrations.django import DjangoIntegration
import dj_database_url

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
SECRET_KEY = os.getenv('SECRET', 'iamnotsecret')
DEBUG = bool(os.getenv('DEBUG'))
ALLOWED_HOSTS = os.getenv('ALLOWED_HOSTS', '0.0.0.0,local.eventowl.com,127.0.0.1').split(',')

INSTALLED_APPS = [
    'concertowl.apps.ConcertowlConfig',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django_q',
    'widget_tweaks'
]

SENTRY_DSN = os.getenv('SENTRY_DSN')
if SENTRY_DSN:
    sentry_sdk.init(
        dsn=SENTRY_DSN,
        release=os.getenv('HEROKU_RELEASE_VERSION', '0'),
        integrations=[DjangoIntegration()]
    )

MIDDLEWARE = ['django.middleware.gzip.GZipMiddleware',
              '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',
              'concertowl.middleware.session_user',
              'whitenoise.middleware.WhiteNoiseMiddleware',
              ]

ROOT_URLCONF = 'eventowl.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',
                'concertowl.context_processors.notifications'
            ],
        },
    },
]

WSGI_APPLICATION = 'eventowl.wsgi.application'

DATABASES = {
    'default': dj_database_url.config(conn_max_age=600)
}

CACHES = {
    "default": {
        "BACKEND": "django_redis.cache.RedisCache",
        "LOCATION": os.getenv('REDIS_URL', 'redis://localhost:6379/1'),
        "OPTIONS": {
            "CLIENT_CLASS": "django_redis.client.DefaultClient",
        }
    }
}

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

LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')

SESSION_SAVE_EVERY_REQUEST = True
SESSION_COOKIE_AGE = 63113904

DATA_UPLOAD_MAX_NUMBER_FIELDS = 2000
GEOIP_PATH = os.path.join(BASE_DIR, 'db.mmdb')

Q_CLUSTER = {
    'name': 'eventowl2',
    'django_redis': 'default',
    'workers': 10,
}

RAVEN_CONFIG = {
    'dsn': os.getenv('SENTRY_DSN'),
    'release': os.getenv('SOURCE_VERSION'),
}
