import json

from django.core.exceptions import ImproperlyConfigured

from .base import *  # NOQA

DEBUG = False
ALLOWED_HOSTS = ['www.sfconservancy.org', 'sfconservancy.org']

ADMINS = [
    ('Bradley M. Kuhn', 'sysadmin@sfconservancy.org'),
    ('Ben Sturmfels', 'sysadmin+conservancy@sturm.com.au'),
]

MANAGERS = [
    ('Bradley M. Kuhn', 'sysadmin@sfconservancy.org'),
]

DATABASES = {
    'default': {
        'NAME': '/var/lib/www/database/conservancy-website.sqlite3',
        'ENGINE': 'django.db.backends.sqlite3',
        # WAL mode allows concurrent reading and writing. It is enabled permanently with:
        # sudo -u www-data sqlite3 conservancy-website.sqlite3 'PRAGMA journal_mode=WAL;'
        'OPTIONS': {
            # Reduce the incidence of "database locked" errors by allowing write
            # requests to be queued for some seconds.
            'timeout': 10,
            'init_command': 'PRAGMA journal_mode = WAL; PRAGMA synchronous = NORMAL',
            # "transaction_mode": "IMMEDIATE",
        },
    }
}

# Apache/mod_wsgi doesn't make it straightforward to pass environment variables
# to Django (can't use the Apache config).
with open(BASE_DIR.parent / 'secrets.json') as f:  # NOQA
    secrets = json.load(f)

def get_secret(secrets, setting):
    try:
        return secrets[setting]
    except KeyError:
        raise ImproperlyConfigured(f'Missing secret \'{setting}\'')

SECRET_KEY = get_secret(secrets, 'SECRET_KEY')

SESSION_COOKIE_SECURE = True

STRIPE_API_KEY = get_secret(secrets, 'STRIPE_API_KEY')
STRIPE_ENDPOINT_SECRET = get_secret(secrets, 'STRIPE_ENDPOINT_SECRET')

CAPTCHA_FLITE_PATH = '/usr/bin/flite'
CAPTCHA_SOX_PATH = '/usr/bin/sox'
CAPTCHA_NOISE_FUNCTIONS = ('captcha.helpers.noise_dots',)