"""
Django settings for core project.

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

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

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

import os
import uuid

from configurations import Configuration, values


class Base(Configuration):
    # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
    BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    VERSION = '0.0.1'


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

    # SECURITY WARNING: keep the secret key used in production secret!
    SECRET_KEY = values.Value(str(uuid.uuid4()))

    # SECURITY WARNING: don't run with debug turned on in production!
    DEBUG = values.BooleanValue(True)

    ALLOWED_HOSTS = values.ListValue(['api.ghrecommender.localhost'])
    SESSION_COOKIE_DOMAIN = values.Value('.ghrecommender.localhost')


    # Application definition

    INSTALLED_APPS = [
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',

        'django_extensions',
        'social_django',
        'rest_framework',
        'corsheaders',

        'subscriptions',
        'core',
    ]

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

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


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

    DATABASES = values.DatabaseURLValue('postgres://github:githubpassword@localhost/github')

    CACHES = values.CacheURLValue('redis://127.0.0.1:6379/1')

    SESSION_ENGINE = 'django.contrib.sessions.backends.signed_cookies'


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

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

    # python-social-auth

    AUTHENTICATION_BACKENDS = (
        'social_core.backends.github.GithubOAuth2',
        'django.contrib.auth.backends.ModelBackend',
    )

    SOCIAL_AUTH_GITHUB_KEY = values.SecretValue()
    SOCIAL_AUTH_GITHUB_SECRET = values.SecretValue()
    SOCIAL_AUTH_GITHUB_SCOPE = ['user:email']
    
    SOCIAL_AUTH_LOGIN_REDIRECT_URL = values.Value('http://ghrecommender.localhost:3000/app/recommendations/')  # Frontend app
    SOCIAL_AUTH_REDIRECT_IS_HTTPS = values.BooleanValue(True)

    # django-rest-framework

    CORS_ORIGIN_ALLOW_ALL = False
    CORS_ALLOW_CREDENTIALS = True
    CORS_ORIGIN_WHITELIST = values.ListValue(['ghrecommender.localhost:3000'])

    REST_FRAMEWORK = {
        'DEFAULT_AUTHENTICATION_CLASSES': (
            'rest_framework.authentication.SessionAuthentication',
            'rest_framework.authentication.BasicAuthentication',
        ),
        'DEFAULT_PERMISSION_CLASSES': (
            'rest_framework.permissions.IsAuthenticated',
        ),
        'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.LimitOffsetPagination',
        'PAGE_SIZE': 100,
        'DEFAULT_FILTER_BACKENDS': ('django_filters.rest_framework.DjangoFilterBackend',),
        'DEFAULT_THROTTLE_CLASSES': (
            'rest_framework.throttling.AnonRateThrottle',
            'rest_framework.throttling.UserRateThrottle',
        ),
        'DEFAULT_THROTTLE_RATES': {
            'anon': '5/minute',
            'user': '100/minute',
        }
    }

    REST_FRAMEWORK_EXTENSIONS = {
        'DEFAULT_CACHE_ERRORS': False,
        'DEFAULT_CACHE_RESPONSE_TIMEOUT': 60 * 1,
        'DEFAULT_OBJECT_CACHE_KEY_FUNC':
            'rest_framework_extensions.utils.default_object_cache_key_func',
    }

    RPC_MODEL_URL = values.Value('http://localhost:4000/jsonrpc')
    MONGO_HOST = values.Value('127.0.0.1')

    ADMIN_URL = values.Value('admin')


class Dev(Base):
    INSTALLED_APPS = Base.INSTALLED_APPS + [
        'debug_toolbar',
    ]

    MIDDLEWARE = Base.MIDDLEWARE + [
        'debug_toolbar.middleware.DebugToolbarMiddleware',
    ]

    INTERNAL_IPS = ['127.0.0.1']


class Prod(Base):
    DEBUG = False

    INSTALLED_APPS = Base.INSTALLED_APPS + [
        'raven.contrib.django.raven_compat',
    ]

    RAVEN_CONFIG = {
        'dsn': values.URLValue(environ_name='SENTRY_DSN'),
        'release': Base.VERSION,
        'environment': 'production',
    }


class Test(Base):
    SOCIAL_AUTH_GITHUB_KEY = '*********'
    SOCIAL_AUTH_GITHUB_SECRET = '*********'

    CACHES = {
        'default': {
            'BACKEND': 'django.core.cache.backends.dummy.DummyCache',
        }
    }
    
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.sqlite3',
            'NAME': 'test_db',
        }
    }

    PASSWORD_HASHERS = (
        'django.contrib.auth.hashers.MD5PasswordHasher',
    )
