from os import path

confdir = path.abspath(path.dirname(__file__))
# use parent dir as dbdir else fallback to current dir
dbdir = path.abspath(path.join(confdir, '..')) if confdir.endswith('conf') \
    else confdir

class BaseConfiguration(object):
    # Make this random (used to generate session keys)
    SECRET_KEY = '74d9e9f9cd40e66fc6c4c2e9987dce48df3ce98542529fd0'
    SQLALCHEMY_DATABASE_URI = 'sqlite:///{0}'.format(path.join(
        dbdir, 'odcs.db'))
    SQLALCHEMY_TRACK_MODIFICATIONS = False

    HOST = '127.0.0.1'
    PORT = 5005

    DEBUG = False
    # Global network-related values, in seconds
    NET_TIMEOUT = 120
    NET_RETRY_INTERVAL = 30

    # Available backends are: console, file, journal.
    LOG_BACKEND = 'journal'

    # Path to log file when LOG_BACKEND is set to "file".
    LOG_FILE = 'odcs.log'

    # Available log levels are: debug, info, warn, error.
    LOG_LEVEL = 'info'

    SSL_ENABLED = False


class DevConfiguration(BaseConfiguration):
    DEBUG = True
    LOG_BACKEND = 'console'
    LOG_LEVEL = 'debug'

    # Global network-related values, in seconds
    NET_TIMEOUT = 5
    NET_RETRY_INTERVAL = 1


class TestConfiguration(BaseConfiguration):
    LOG_BACKEND = 'console'
    LOG_LEVEL = 'debug'
    DEBUG = True

    SQLALCHEMY_DATABASE_URI = 'sqlite:///{0}'.format(
        path.join(dbdir, 'tests', 'test_odcs.db'))

    # Global network-related values, in seconds
    NET_TIMEOUT = 3
    NET_RETRY_INTERVAL = 1


class ProdConfiguration(BaseConfiguration):
    AUTH_BACKEND = 'openidc'
    AUTH_OPENIDC_REQUIRED_SCOPES = [
        'openid',
        'https://id.fedoraproject.org/scope/groups',
        'https://pagure.io/odcs/new-compose',
        'https://pagure.io/odcs/renew-compose',
        'https://pagure.io/odcs/delete-compose',
    ]
    OIDC_BASE_NAMESPACE = 'https://pagure.io/odcs/'

{% if env == 'staging' %}
    AUTH_OPENIDC_USERINFO_URI = 'https://id.stg.fedoraproject.org/openidc/UserInfo'
    SECRET_KEY = "{{ odcs_stg_secret_key }}"

    SQLALCHEMY_DATABASE_URI = 'postgresql+psycopg2://odcs:{{odcs_stg_db_password}}@db-odcs/odcs{{ '?sslmode=require' if odcs_force_postgres_ssl else '' }}'

    KOJI_PROFILE = 'odcs_stg'

    MBS_URL ='http://mbs.stg.fedoraproject.org/module-build-service'

    KOJI_KRB_PRINCIPAL = 'odcs/odcs.stg.fedoraproject.org@STG.FEDORAPROJECT.ORG'

{% else %}
    AUTH_OPENIDC_USERINFO_URI = 'https://id.fedoraproject.org/openidc/UserInfo'
    SECRET_KEY = "{{ odcs_prod_secret_key }}"

    SQLALCHEMY_DATABASE_URI = 'postgresql+psycopg2://odcs:{{odcs_prod_db_password}}@db-odcs/odcs{{ '?sslmode=require' if odcs_force_postgres_ssl else '' }}'

    KOJI_PROFILE = 'odcs_production'

    MBS_URL = 'http://mbs.fedoraproject.org/module-build-service'

    KOJI_KRB_PRINCIPAL = 'odcs/odcs.stg.fedoraproject.org@STG.FEDORAPROJECT.ORG'
{% endif %}

    KOJI_KRB_KEYTAB = '/etc/krb5.odcs_odcs{{env_suffix}}.fedoraproject.org.keytab'
    KOJI_KRB_CCACHE = None

    TARGET_DIR = "{{ odcs_target_dir }}"
    TARGET_DIR_URL = "{{ odcs_target_dir_url }}"

    EXTRA_TARGET_DIRS = {{ odcs_extra_target_dirs }}

    ALLOWED_SOURCE_TYPES = {{ odcs_allowed_source_types }}
    RAW_CONFIG_URLS = {{ odcs_raw_config_urls }}

    ALLOWED_CLIENTS = {
        'groups': {{ odcs_allowed_clients_groups }},
        'users': {{ odcs_allowed_clients_users }},
    }

    ADMINS = {
        'groups': {{ odcs_admin_groups }},
        'users': {{ odcs_admin_users }},
    }

    SIGKEYS = {{ odcs_sigkeys }}

    MESSAGING_BACKEND = "{{ odcs_messaging_backend }}"
    MESSAGING_BROKER_URLS = {{ odcs_messaging_producer_broker_urls }}
    MESSAGING_CERT_FILE = "/etc/ssl/odcs/umb-client.crt"
    MESSAGING_KEY_FILE = "/etc/ssl/odcs/umb-client.key"
    MESSAGING_CA_CERT = "{{ odcs_messaging_ca_cert }}"
{% if env == "staging" %}
    MESSAGING_TOPIC_PREFIX = "org.fedoraproject.stg."
{% else %}
    MESSAGING_TOPIC_PREFIX = "org.fedoraproject.prod."
{% endif %}
    MESSAGING_TOPIC = "odcs.compose.state-changed"
    INTERNAL_MESSAGING_TOPIC = "odcs.internal.msg"

    PUNGI_KOJI_ARGS = ['--no-latest-link']
    PUNGI_TIMEOUT=10800

    PUNGI_RUNROOT_ENABLED = {{ odcs_pungi_runroot_enabled }}
    PUNGI_PARENT_RUNROOT_CHANNEL = "{{ odcs_pungi_parent_runroot_channel }}"
    PUNGI_PARENT_RUNROOT_PACKAGES = {{ odcs_pungi_parent_runroot_packages }}
    PUNGI_PARENT_RUNROOT_MOUNTS = {{ odcs_pungi_parent_runroot_mounts }}
    PUNGI_PARENT_RUNROOT_WEIGHT = {{ odcs_pungi_parent_runroot_weight }}
    PUNGI_PARENT_RUNROOT_TAG = "{{ odcs_pungi_parent_runroot_tag }}"
    PUNGI_PARENT_RUNROOT_ARCH = "{{ odcs_pungi_parent_runroot_arch }}"
    PUNGI_RUNROOT_TARGET_DIR = "{{ odcs_pungi_runroot_target_dir }}"
    PUNGI_RUNROOT_TARGET_DIR_URL = "{{ odcs_pungi_runroot_target_dir_url }}"

{% if odcs_allowed_source_types %}
    ALLOWED_SOURCE_TYPES = {{ odcs_allowed_source_types }}
{% endif %}
{% if odcs_allowed_flags %}
    ALLOWED_FLAGS = {{ odcs_allowed_flags }}
{% endif %}
{% if odcs_allowed_arches %}
    ALLOWED_ARCHES = {{ odcs_allowed_arches }}
{% endif %}
{% if odcs_allowed_results %}
    ALLOWED_RESULTS = {{ odcs_allowed_results }}
{% endif %}
{% if odcs_allowed_sources %}
    ALLOWED_SOURCES = {{ odcs_allowed_sources }}
{% endif %}

    CELERY_BROKER_URL = "amqps://odcs-private-queue{{ env_suffix }}@rabbitmq01{{ env_suffix }}.iad2.fedoraproject.org//odcs"
    CELERY_CONFIG = {
        'certfile': "/etc/odcs/odcs-private-queue.crt",
        'keyfile': "/etc/odcs/odcs-private-queue.key",
        'ca_certs': "/etc/odcs/ca.crt",
        'broker_login_method': "EXTERNAL",
    }

{% if odcs_celery_router_config %}
    CELERY_ROUTER_CONFIG = {{ odcs_celery_router_config }}
{% endif %}

    EXPECTED_BACKEND_NUMBER = {{ odcs_expected_backend_number }}

