""" Django production settings for console project. """
import os

import dotenv

BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

orig_user_env = dict(os.environ.items())

dotenv.load_dotenv(dotenv_path=os.path.join(BASE_DIR, '.env.prod'))

if os.path.exists('/etc/armaconsole/env.prod'):
    dotenv.load_dotenv(dotenv_path='/etc/armaconsole/env.prod')

# Convert DBC variables to our names
dbc_conf = dotenv.dotenv_values(dotenv_path=os.environ.get('DEBCONF_DBCONF_FPATH'))
debconf_dbconfig_django_map = (
    ('dbname', 'POSTGRES_DB'),
    ('dbuser', 'POSTGRES_USER'),
    ('dbpass', 'POSTGRES_PASSWORD'),
    ('dbserver', 'POSTGRES_HOST'),
    ('dbport', 'POSTGRES_PORT'),
)
for debconf_val, django_val in debconf_dbconfig_django_map:
    os.environ[django_val] = dbc_conf.get(debconf_val, os.environ.get(django_val))

# Restore original environment, as user environment is more important
os.environ.update(orig_user_env)

# noinspection PyUnresolvedReferences
from .base import *

# Security key for import/export firewall config
SECRET_KEY = os.environ.get('SECRET_KEY', '')

CACHES = {
    'default': {
        'BACKEND': 'django_redis.cache.RedisCache',
        'LOCATION': f'redis://{REDIS_HOST}:{REDIS_PORT}/1',
        'OPTIONS': {
            'CLIENT_CLASS': 'django_redis.client.DefaultClient'
        }
    },
    'local': {
        'BACKEND': 'django_redis.cache.RedisCache',
        'LOCATION': f'redis://{REDIS_HOST}:{REDIS_PORT}/1',
        'OPTIONS': {
            'CLIENT_CLASS': 'django_redis.client.DefaultClient'
        }
    },
    'redis': {
        'BACKEND': 'django_redis.cache.RedisCache',
        'LOCATION': f'redis://{REDIS_HOST}:{REDIS_PORT}/1',
        'OPTIONS': {
            'CLIENT_CLASS': 'django_redis.client.DefaultClient'
        }
    }
}
