"""
Django settings for barista project.

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

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

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

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

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

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'l-!ij0l@jduk^3w78sy5%wkx%_47kjo8c_myzuiqcz27kj_qzc'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = ['*']


# Application definition

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

    # Our own apps
    'webcam.apps.WebCamConfig',
    'recognition.apps.RecognitionConfig',
    'coffeestatus.apps.CoffeeStatusConfig',
]

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

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


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

DATABASES = {
    # Update database configuration with $DATABASE_URL.
    'default': dj_database_url.config(
        conn_max_age=500,
        default='sqlite:///db.sqlite3',
    )
}


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

STATIC_URL = '/static/'

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

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

# Saved files
MEDIA_ROOT = os.environ.get('MEDIA_PATH', os.path.join(BASE_DIR, "media"))
MEDIA_URL = '/media/'

# Logging settings
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'console': {
            'class': 'logging.StreamHandler',
        },
    },
    'loggers': {
        '': {
            'handlers': ['console'],
            'level': 'INFO',
        },
    },
}

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': (),
    'DEFAULT_PERMISSION_CLASSES': (
        'rest_framework.permissions.AllowAny',
    ),
}

# The token required by the command API, taken from the environment variable.
# If undefined, then accept all the requests (useful when developing).
SLACK_COMMAND_TOKEN = os.environ.get('SLACK_COMMAND_TOKEN', None)

# The Slack incoming webhook URL to which the information about the fresh coffee is POSTed
SLACK_WEBHOOK_URL = os.environ.get('SLACK_WEBHOOK_URL', None)

# When the automatic snapshots should be taken?
SNAPSHOT_SCHEDULE_TIMEZONE = os.environ.get('SNAPSHOT_SCHEDULE_TIMEZONE') or 'UTC'
SNAPSHOT_SCHEDULE_INTERVAL = int(os.environ.get('SNAPSHOT_SCHEDULE_INTERVAL') or '1')
SNAPSHOT_SCHEDULE_START_TIME = os.environ.get('SNAPSHOT_SCHEDULE_START_TIME') or '00:00'
SNAPSHOT_SCHEDULE_END_TIME = os.environ.get('SNAPSHOT_SCHEDULE_END_TIME') or '23:59'
SNAPSHOT_SCHEDULE_WEEKDAYS = os.environ.get('SNAPSHOT_SCHEDULE_WEEKDAYS') or '1,2,3,4,5,6,7'

# The AWS access key used to access the storage buckets.
AWS_ACCESS_KEY_ID = os.environ["AWS_ACCESS_KEY_ID"]

# The AWS secret access key used to access the storage buckets.
AWS_SECRET_ACCESS_KEY = os.environ["AWS_SECRET_ACCESS_KEY"]

# The S3 bucket used to store uploaded files.
AWS_S3_BUCKET_NAME = os.environ["AWS_S3_BUCKET_NAME"]

# The region to connect to when storing files.
AWS_REGION = os.environ["AWS_REGION"]

# A prefix to add to the start of all uploaded files.
AWS_S3_KEY_PREFIX = os.environ.get("AWS_S3_KEY_PREFIX") or "media"

# Whether to enable querystring authentication for uploaded files.
AWS_S3_BUCKET_AUTH = False

# The S3 bucket used to store static files.
AWS_S3_BUCKET_NAME_STATIC = AWS_S3_BUCKET_NAME

# Whether to enable querystring authentication for static files.
AWS_S3_BUCKET_AUTH_STATIC = False

# A prefix to add to the start of all static files.
AWS_S3_KEY_PREFIX_STATIC = os.environ.get("AWS_S3_KEY_PREFIX_STATIC") or "static"

DEFAULT_FILE_STORAGE = "django_s3_storage.storage.S3Storage"
