"""
Django settings for webapp project.

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

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
from os import path
from getpass import getuser
import requests
from urllib.parse import urlparse

# 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.10/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '-uars0ga6yf0&nbagj%z-&wz)+kvw$b$wc=q_i52_@-9a9yg30'

# SECURITY WARNING: don't run with debug turned on in production!
if getuser() == 'ubuntu':
    DEBUG = False
else:
    DEBUG = True

ALLOWED_HOSTS = []

if DEBUG:
    ALLOWED_HOSTS.append('127.0.0.1')
    r = requests.get('http://localhost:4040/api/tunnels')
    tunnels = r.json()['tunnels']
    found_ngrok = False
    for tunnel in tunnels:
        print(tunnel['name'])
        # assumes we only run django on port 8000
        if tunnel['config']['addr'].split(':')[-1] == '8000':
            ngrok_host = urlparse(tunnel['public_url']).hostname
            print('Found ngrok tunnel at {} and added it to ALLOWED_HOSTS'.format(ngrok_host))
            ALLOWED_HOSTS.append(ngrok_host)
            found_ngrok = True
    if not found_ngrok:
        print('No ngrok tunnels found; developing webhooks (eg for Twilio) may be difficult')
else:
    ALLOWED_HOSTS.append('54.183.66.153')
    ALLOWED_HOSTS.append('evoke.ai')
    ALLOWED_HOSTS.append('www.evoke.ai')

# SQS
#if DEBUG:
#    RECORDING_QUEUE = '{}-0'.format(getuser())
#else:
RECORDING_QUEUE = 'prod-worker-queue'

# Application definition

INSTALLED_APPS = [
    'main.apps.MainConfig',
    'twilio_caller.apps.TwilioCallerConfig',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

MIDDLEWARE = [
    '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',
]

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


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

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}


# 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



# Logging
# https://docs.djangoproject.com/en/1.10/topics/logging/#configuring-logging

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'file': {
            'level': 'DEBUG',
            'class': 'logging.FileHandler',
            'filename': path.expanduser('~/logs/keystone.log'),
        },
        'console': {
            'class': 'logging.StreamHandler',
        },
    },
    'loggers': {
        'django': {
            'handlers': ['file', 'console'],
            'level': 'INFO',
            'propagate': 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/")

# Key phrases to search speech for

KEY_PHRASES = {'action item':{'type':'after'},
               'thats fascinating':{'type':'before'},
               'remember':{'type':'before'},
               'take a note on that': {'type':'before'}}
